Chapter 09 Pointers
-
Upload
cuong-polyme -
Category
Documents
-
view
227 -
download
0
Transcript of Chapter 09 Pointers
-
8/13/2019 Chapter 09 Pointers
1/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Chapter 9:
Pointers
-
8/13/2019 Chapter 09 Pointers
2/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
9.1Getting the Address of a Variable
-
8/13/2019 Chapter 09 Pointers
3/55
Copyright 2012 Pearson Education, Inc.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 ofa variable
int num = -99;
cout
-
8/13/2019 Chapter 09 Pointers
4/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
9.!Pointer Variables
-
8/13/2019 Chapter 09 Pointers
5/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Pointer Variables
Pointer variable "ften #ust called apointer$ it%s a variable that holds anaddress
&ecause a pointer variable holds theaddress of another piece of data$ it 'points'
to the data
-
8/13/2019 Chapter 09 Pointers
6/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
(omething )i*e Pointers Arra+s
,e have alread+ -or*ed -ith something similarto pointers$ -hen -e learned to pass arra+s asarguments to functions.
or e/ample$ suppose -e use this statement topass the arra+ numbersto the showValuesfunction
showValues(numbers, SI!";
-
8/13/2019 Chapter 09 Pointers
7/55Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
(omething )i*e Pointers Arra+s
0he #aluesparameter$ in the showValues
function$ points to the numbersarra+.
22 automaticall+ storesthe address of numbersin
the #aluesparameter.
-
8/13/2019 Chapter 09 Pointers
8/55Copyright 2012 Pearson Education, Inc.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
-
8/13/2019 Chapter 09 Pointers
9/55Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
(omething )i*e Pointers
3eference Variables
0he donutsparameter$ in the $et%rderfunction$
points to the ell).onutsvariable.
22 automaticall+ stores
the address ofell).onuts in the
donutsparameter.
-
8/13/2019 Chapter 09 Pointers
10/55Copyright 2012 Pearson Education, Inc.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 andreference variables.
0his means +ou are responsible for finding the
address +ou -ant to store in the pointer and
correctl+ using it.
-
8/13/2019 Chapter 09 Pointers
11/55Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Pointer Variables
5efinition
int intptr;
3ead as6intptrcan hold the address of an int7
(pacing in definition does not matter
int intptr; // same as abo#eint intptr; // same as abo#e
-
8/13/2019 Chapter 09 Pointers
12/55Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Pointer Variables
Assigning an address to a pointer variableint intptr;
intptr = #
8emor+ la+outnum intptr
01 2x3a22
address of num 2x3a22
-
8/13/2019 Chapter 09 Pointers
13/55Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
-
8/13/2019 Chapter 09 Pointers
14/55Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
0he ndirection "perator
0he indirection operator :; dereferences
a pointer.
t allo-s +ou to access the item that the
pointer points to.
int x = 01;
int intptr = &x;
cout
-
8/13/2019 Chapter 09 Pointers
15/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
-
8/13/2019 Chapter 09 Pointers
16/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.941=
-
8/13/2019 Chapter 09 Pointers
17/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
9.>0he 3elationship &et-een Arra+s
and Pointers
-
8/13/2019 Chapter 09 Pointers
18/55
Copyright 2012 Pearson Education, Inc.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;
cout
-
8/13/2019 Chapter 09 Pointers
19/55
Copyright 2012 Pearson Education, Inc.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
-
8/13/2019 Chapter 09 Pointers
20/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.94!?
-
8/13/2019 Chapter 09 Pointers
21/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Pointers in E/pressions
Givenint #als45=3,6,77, #alptr;
#alptr = #als;
,hat is #alptr 8 7@ t means :address in
#alptr; 2 :1 siBe of an int;
cout
-
8/13/2019 Chapter 09 Pointers
22/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Arra+ Access
Arra+ elements can be accessed in man+ -a+s
Arra+ access method E/ample
arra+ name and 45 #als405 = 76;
pointer to arra+ and 45 #alptr405 = 76;
arra+ name and subscript
arithmetic
(#als 8 0" = 76;
pointer to arra+ andsubscript arithmetic
(#alptr 8 0" = 76;
-
8/13/2019 Chapter 09 Pointers
23/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Arra+ Access
onversion #als4i5is equivalent to
(#als 8 i"
Co bounds chec*ing performed on arra+access$ -hether using arra+ name or a
pointer
-
8/13/2019 Chapter 09 Pointers
24/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
rom Program 94D
-
8/13/2019 Chapter 09 Pointers
25/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
9.Pointer Arithmetic
-
8/13/2019 Chapter 09 Pointers
26/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
Pointer Arithmetic
"perations on pointer variables
94!=
"peration E/ampleint #als45=3,6,77;int #alptr = #als;
88, -- #alptr88; // points at 6#alptr--; // now points at 3
8, - :pointer and int; cout
-
8/13/2019 Chapter 09 Pointers
27/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
rom Program 949
-
8/13/2019 Chapter 09 Pointers
28/55
Copyright 2012 Pearson Education, Inc.Copyright 2012 Pearson Education, Inc.
9.