# docx

Λογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 4 χρόνια και 6 μήνες)

78 εμφανίσεις

Starting Out with C++: From Control Structures through Objects, 7/E

-

Tony Gaddis

Ray Devore's Notes

on
CHAPTER 9 Pointers

Page
1

of
4

CHAPTER 9 Pointers

................................
................................
................................
............

491

9.1

Getting the Address of a Variable

................................
................................
...............

491

int abc;

std::cout << "The address of abc is " << &abc << std::endl;

The & is called the "address of" operator.

9.2

Pointer Variables

................................
................................
................................
.......

493

int *ptr; // the variable ptr

is a pointer to an int

ptr = &abc; // assigns the address of abc to the variable ptr

*ptr = 7;
// assigns the value 7 to the location pointed to by ptr

The * is used to define a pointer variable.

The * is used to dereference a pointer, that is, get the

value at the pointer/address

9.3

The Relationship Between Arrays and Pointers

................................
...........................

500

int arr[10];

The variable arr is a pointer to the first element of the array.

*arr

will give the value of the first position of the array

When you pass an array to a
function, you are actually passing a copy of the address or a
copy of the pointer.

Starting Out with C++: From Control Structures through Objects, 7/E

-

Tony Gaddis

Ray Devore's Notes

on
CHAPTER 9 Pointers

Page
2

of
4

9.4

Pointer Arithmetic

................................
................................
................................
.....

504

*(arr + 0)

will give the value of the first position of the array, equivalent to
arr[0]

*(arr + 4)

will give the value of the fifth position
of the array, equivalent to
arr[4]

ptr = arr + 4;

*ptr will give the value of the fifth position of the array, equivalent to arr[4]

9.5

Initializing Pointers

................................
................................
................................
....

506

When you define a pointer variable there are a few ways to initialize it.

1) we

can create what is called a null pointer

int *nullPtr = 0;

Zero is the only literal value that can be assigned to a pointer variable.

2) We can create a pointer to a known variable

int abc;

int *abcPtr = &abc;

We will discuss another method later in
class.

9.6

Comparing Pointers

................................
................................
................................
....

507

If you compare pointer variables, you are actually comparing the addresses that they are
pointing to.

if(ptr1 == ptr2) // compares the values of the pointers

You must dereference the pointers to get at the values poin
ted to

if(*ptr1 == *ptr2) // compares the values of the pointers

Starting Out with C++: From Control Structures through Objects, 7/E

-

Tony Gaddis

Ray Devore's Notes

on
CHAPTER 9 Pointers

Page
3

of
4

9.7

Pointers as Function Parameters

................................
................................
................

509

void func(int *, int); // prototype

void func(int*ptr, int size) // header

{

for(int ndx = 0; ndx < size; ++ndx)

std::cout << *(ptr + ndx) << " "

<< std::flush;

std::cout << std::endl;

}

const int SIZE = 15;

int abc[SIZE];

func(abc, SIZE); // function call

9.8

Focus on Software Engineering: Dynamic Memory Allocation

................................
.....

518

A third method to initialize a pointer

int *ptr3 = new int[25];

The above
statement dynamically allocates contiguous space for 25 integers and assigns the
starting location to ptr3.

You can also assign new space to a pointer.

int *ptr4;

int size = 14;

ptr4 = new int[size]; // allocates contiguous space for "size"

//
integers

and assigns the starting location to ptr4.

ptr4 = new int; // allocates space for a single int and

//
assigns the address to ptr4;

Anytime you dynamically allocate memory, you need to free up the memory for other
programs to use. This is performed with

a delete statement.

delete ptr4[]; // if ptr4 was allocated as an array

delete ptr4; // if ptr4 was allocated to a single value

Problems with dynamic allocation:

1) allocating new space to a pointer without freeing the previously allocated space

ptr4 = new int[50];

ptr4 = new int [32]; // keeps the previous 50 allocated,

//
but no way to access it.

Starting Out with C++: From Control Structures through Objects, 7/E

-

Tony Gaddis

Ray Devore's Notes

on
CHAPTER 9 Pointers

Page
4

of
4

9.9

Focus on Software Engineering: Returning Pointers from Functions

............................

522

int*

create
(int); // prototype

int*

create
(int size) // header

{

int

*newArr = new int[size];

for(int ndx = 0; ndx < size; ++ndx)

*(
newArr

+ ndx)
= 0;

return newArr
;

}

int other()

{

int
*
abc;

abc = create(10);

// function call

}

9.10

Focus on Problem Solving and Program Design: A Case Study

................................
.....

529

For the student to
study