Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Pointers
9.1
Getting the Address of a Variable
9.!
Pointer Variables
Pointer Variables
Pointer variable "ften #ust called a pointer$ it%s a variable that holds an address &ecause a pointer variable holds the address of another piece of data$ it 'points' to the data
Pointer Variables
Pointer variables are +et another -a+ using a memor+ address to -or* -ith a piece of data. Pointers are more 'lo-4level' than arra+s and reference variables. 0his means +ou are responsible for finding the address +ou -ant to store in the pointer and correctl+ using it.
Pointer Variables
5efinition
int /intptr;
3ead as 6intptr can hold the address of an int7 (pacing in definition does not matter
int / intptr; int/ intptr; // same as abo#e // same as abo#e
Pointer Variables
Assigning an address to a pointer variable
int /intptr; intptr = #
8emor+ la+out
num
01 address of num 2x3a22
intptr
2x3a22
941=
Copyright 2012 Pearson Education, Inc.
9.>
0he 3elationship &et-een Arra+s and Pointers
77
94!?
Copyright 2012 Pearson Education, Inc.
Pointers in E/pressions
Given
int #als45=&3,6,77,, /#alptr; #alptr = #als;
Arra+ Access
Arra+ elements can be accessed in man+ -a+s
Arra+ access method arra+ name and 45 pointer to arra+ and 45 arra+ name and subscript arithmetic pointer to arra+ and subscript arithmetic E/ample #als405 = 76; #alptr405 = 76; /(#als 8 0" = 76; /(#alptr 8 0" = 76;
Arra+ Access
1onversion #als4i5 is equivalent to /(#als 8 i" Co bounds chec*ing performed on arra+ access$ -hether using arra+ name or a pointer
9.E
Pointer Arithmetic
Pointer Arithmetic
"perations on pointer variables
"peration
88, -8, - :pointer and int; 8=, -= :pointer and int; - :pointer from pointer;
E/ample
int #als45=&3,6,77,; int /#alptr = #als; #alptr88; // points at 6 #alptr--; // now points at 3 cout << /(#alptr 8 0"; // 77 #alptr = #als; // points at 3 #alptr 8= 0; // points at 77 cout << #alptr9#al; // di::erence //(number o: ints" between #alptr // and #al
94!=
9.<
9nitialiBing Pointers
9nitialiBing Pointers
1an initialiBe at definition time
int num, /numptr = # int #al4;5, /#alptr = #al;
9.=
1omparing Pointers
1omparing Pointers
3elational operators :<$ +=$ etc.; can be used to compare addresses in pointers 1omparing addresses in pointers is not the same as comparing contents pointed at b+ pointers
i: (ptr7 == ptr0" // // i: (/ptr7 == /ptr0" // // compares addresses compares contents
94>1
Copyright 2012 Pearson Education, Inc.
9.D
Pointers as .unction Parameters
asteris* A on parameter in protot+pe and heading asteris* * in bod+ to dereference the pointer address as argument to the function
// pass address o: num to $et?um
cin ++ /ptr;
$et?um(&num";
E/ample
#oid swap(int /x, int /)" & int temp; temp = /x; /x = /); /) = temp; , int num7 = 0, num0 = -;; swap(&num7, &num0";
(Program Continues)
Copyright 2012 Pearson Education, Inc.
Pointers to 1onstants
9f -e -ant to store the address of a constant in a pointer$ then -e need to store it in a pointer4to4const.
Pointers to 1onstants
E/ample (uppose -e have the follo-ing definitions
const int SI ! = @; const double pa)Aates4SI !5 = & 7B>11, 76>31, 70>B1, 73>96, 72>;1, 7B>B9 ,;
Pointers to 1onstants
(uppose -e -ish to pass the pa)Aates arra+ to a function@ Here%s an e/ample of ho- -e can do it.
#oid displa)Ca)Aates(const double /rates, int siDe" & :or (int count = 2; count < siDe; count88" & cout << 'Ca) rate :or emplo)ee ' << (count 8 7" << ' is E' << /(rates 8 count" << endl; , ,
0he parameter$ rates$ is a pointer to const double.
Copyright 2012 Pearson Education, Inc.
1onstant Pointers
A constant pointer is a pointer that is initialiBed -ith an address$ and cannot point to an+thing else. E/ample int #alue = 00; int / const ptr = &#alue;
Copyright 2012 Pearson Education, Inc.
1onstant Pointers
9.G
5+namic 8emor+ Allocation
Notice that in line 49 the value 0 is assigned to the sales pointer. It is a good practice to store 0 in a pointer variable after using delete on it. First it prevents code from inadvertentl! using the pointer to access the area of memor! that "as freed. #econd it prevents errors from occurring if delete is accidentall! called on the pointer again. $he delete operator is designed to have no effect "hen used on a null pointer.
Copyright 2012 Pearson Education, Inc.
9.9
3eturning Pointers from .unctions
0he function must not return a pointer to a local variable in the function. A function should onl+ return a pointer
I to data that -as passed to the function as an argument$ or I to d+namicall+ allocated memor+