Sei sulla pagina 1di 55

Chapter 9:

Pointers

Copyright 2012 Pearson Education, Inc.

9.1
Getting the Address of a Variable

Copyright 2012 Pearson Education, Inc.

Getting the Address of a Variable


Each variable in program is stored at a unique address Use address operator & to get address of a variable
int num = -99; cout << &num; // prints address // in hexadecimal

Copyright 2012 Pearson Education, Inc.

9.!
Pointer Variables

Copyright 2012 Pearson Education, Inc.

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

Copyright 2012 Pearson Education, Inc.

(omething )i*e Pointers Arra+s


,e have alread+ -or*ed -ith something similar to pointers$ -hen -e learned to pass arra+s as arguments to functions. .or e/ample$ suppose -e use this statement to pass the arra+ numbers to the showValues function showValues(numbers, SI !";

Copyright 2012 Pearson Education, Inc.

(omething )i*e Pointers Arra+s


0he #alues parameter$ in the showValues function$ points to the numbers arra+.

122 automaticall+ stores the address of numbers in the #alues parameter.

Copyright 2012 Pearson Education, Inc.

(omething )i*e Pointers 3eference Variables


,e have also -or*ed -ith something li*e pointers -hen -e learned to use reference variables. (uppose -e have this function
#oid $et%rder(int &donuts" & cout << '(ow man) dou$hnuts do )ou want* '; cin ++ donuts; ,

And -e call it -ith this code


int -ell).onuts; $et%rder(-ell).onuts";

Copyright 2012 Pearson Education, Inc.

(omething )i*e Pointers 3eference Variables


0he donuts parameter$ in the $et%rder function$ points to the -ell).onuts variable.

122 automaticall+ stores the address of -ell).onuts in the donuts parameter.

Copyright 2012 Pearson Education, Inc.

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.

Copyright 2012 Pearson Education, Inc.

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

Copyright 2012 Pearson Education, Inc.

Pointer Variables
Assigning an address to a pointer variable
int /intptr; intptr = &num;

8emor+ la+out
num
01 address of num 2x3a22

intptr

2x3a22

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

0he 9ndirection "perator


0he indirection operator :/; dereferences a pointer. 9t allo-s +ou to access the item that the pointer points to. int x = 01; int /intptr = &x; cout << /intptr << endl;
0his prints !<.
Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

941=
Copyright 2012 Pearson Education, Inc.

9.>
0he 3elationship &et-een Arra+s and Pointers

Copyright 2012 Pearson Education, Inc.

0he 3elationship &et-een Arra+s and Pointers


Arra+ name is starting address of arra+
int #als45 = &3, 6, 77,; 3
starting address of #als 2x3a22

77

cout << #als; cout << #als425;

// displa)s // 2x3a22 // displa)s 3

Copyright 2012 Pearson Education, Inc.

0he 3elationship &et-een Arra+s and Pointers


Arra+ name can be used as a pointer constant
int #als45 = &3, 6, 77,; cout << /#als; // displa)s 3

Pointer can be used as an arra+ name


int /#alptr = #als; cout << #alptr475; // displa)s 6

Copyright 2012 Pearson Education, Inc.

94!?
Copyright 2012 Pearson Education, Inc.

Pointers in E/pressions
Given
int #als45=&3,6,77,, /#alptr; #alptr = #als;

,hat is #alptr 8 7@ 9t means :address in #alptr; 2 :1 A siBe of an int;


cout << /(#alptr87"; //displa)s 6 cout << /(#alptr80"; //displa)s 77

8ust use ( " as sho-n in the e/pressions


Copyright 2012 Pearson Education, Inc.

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;

Copyright 2012 Pearson Education, Inc.

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

Copyright 2012 Pearson Education, Inc.

.rom Program 94D

Copyright 2012 Pearson Education, Inc.

9.E
Pointer Arithmetic

Copyright 2012 Pearson Education, Inc.

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!=

Copyright 2012 Pearson Education, Inc.

.rom Program 949

Copyright 2012 Pearson Education, Inc.

9.<
9nitialiBing Pointers

Copyright 2012 Pearson Education, Inc.

9nitialiBing Pointers
1an initialiBe at definition time
int num, /numptr = &num; int #al4;5, /#alptr = #al;

1annot mi/ data t+pes


double cost; int /ptr = &cost; // wonFt wor<

1an test for an invalid address for ptr -ith


i: (=ptr" >>>
Copyright 2012 Pearson Education, Inc.

9.=
1omparing Pointers

Copyright 2012 Pearson Education, Inc.

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

Copyright 2012 Pearson Education, Inc.

Pointers as .unction Parameters


A pointer can be a parameter ,or*s li*e reference variable to allo- change to argument from -ithin function 3equires
1; !; >;

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

#oid $et?um(int /ptr"; // ptr is pointer to an int

cin ++ /ptr;
$et?um(&num";

Copyright 2012 Pearson Education, Inc.

E/ample
#oid swap(int /x, int /)" & int temp; temp = /x; /x = /); /) = temp; , int num7 = 0, num0 = -;; swap(&num7, &num0";

Copyright 2012 Pearson Education, Inc.

(Program Continues)
Copyright 2012 Pearson Education, Inc.

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.

Copyright 2012 Pearson Education, Inc.

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 ,;

9n this code$ pa)Aates is an arra+ of constant doubles.


94>G
Copyright 2012 Pearson Education, Inc.

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.

5eclaration of a Pointer to 1onstant

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

Copyright 2012 Pearson Education, Inc.

1onstant Pointers to 1onstants


A constant pointer to a constant is
I a pointer that points to a constant I a pointer that cannot point to an+thing e/cept -hat it is pointing to

E/ample int #alue = 00; const int / const ptr = &#alue;

Copyright 2012 Pearson Education, Inc.

1onstant Pointers to 1onstants

Copyright 2012 Pearson Education, Inc.

9.G
5+namic 8emor+ Allocation

Copyright 2012 Pearson Education, Inc.

5+namic 8emor+ Allocation


1an allocate storage for a variable -hile program is running 1omputer returns address of ne-l+ allocated variable Uses new operator to allocate memor+
double /dptr; dptr = new double;

F new returns address of memor+ location


Copyright 2012 Pearson Education, Inc.

5+namic 8emor+ Allocation


1an also use new to allocate arra+ const int SI ! = 01; arra)Ctr = new double4SI !5; 1an then use 45 or pointer arithmetic to access arra+
:or(i = 2; i < SI !; i88" /arra)ptr4i5 = i / i; or :or(i = 2; i < SI !; i88" /(arra)ptr 8 i" = i / i;

Program -ill terminate if not enough memor+ available to allocate

Copyright 2012 Pearson Education, Inc.

3eleasing 5+namic 8emor+


Use delete to free d+namic memor+
delete :ptr;

Use 45 to free d+namic arra+


delete 45 arra)ptr;

"nl+ use delete -ith d+namic memor+J

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

Copyright 2012 Pearson Education, Inc.

Program 941E :1ontinued;

Copyright 2012 Pearson Education, Inc.

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

Copyright 2012 Pearson Education, Inc.

3eturning Pointers from .unctions


Pointer can be the return t+pe of a function
int/ new?um(";

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+

Copyright 2012 Pearson Education, Inc.

.rom Program 941<

Copyright 2012 Pearson Education, Inc.

Potrebbero piacerti anche