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
• Every variable in an executing program is
allocated a section of memory large
enough to hold a value of that variable’s
type.
• Current C++ compilers that run on PCs
usually allocate a single byte to variables
of type char, two bytes to variables of type
short, four bytes to variables of type float ,
long and int, and 8 bytes to variables of
type double.
Copyright © 2012 Pearson Education, Inc.
Getting the Address of a
Variable
• A variable’s address is the address of the
first byte allocated to that variable.
• Suppose that the following variables are
defined in a program:
char letter; short number; float amount;
• they might be arranged in memory like
this:
Copyright © 2012 Pearson Education, Inc.
Getting the Address of a
Variable
• C++ has an address operator & that can
be used to retrieve the address of any
variable.
• To use it, place it before the variable
whose address you want.
• Here is an expression that returns the
address of the variable amount:
&amount
Copyright © 2012 Pearson Education, Inc.
Getting the Address of a
Variable
• Use address operator & to get address of
a variable:
int num = -99;
cout << # // prints address
// in hexadecimal
Copyright © 2012 Pearson Education, Inc.
9.2
Pointer Variables
Copyright © 2012 Pearson Education, Inc.
Pointer Variables
• Pointer variable : Often just called a
pointer, it's a variable that holds an
address
• Because a pointer variable holds the
address of another piece of data, it "points"
to the data
Copyright © 2012 Pearson Education, Inc.
Something Like Pointers: Arrays
• We have already worked with something similar
to pointers, when we learned to pass arrays as
arguments to functions.
• For example, suppose we use this statement to
pass the array numbers to the showValues
function:
showValues(numbers, SIZE);
Copyright © 2012 Pearson Education, Inc.
Something Like Pointers : Arrays
The values parameter, in the showValues
function, points to the numbers array.
C++ automatically stores
the address of numbers in
the values parameter.
Copyright © 2012 Pearson Education, Inc.
Something Like Pointers:
Reference Variables
• We have also worked with something like pointers
when we learned to use reference variables.
Suppose we have this function:
void getOrder(int &donuts)
{
cout << "How many doughnuts do you want? ";
cin >> donuts;
}
• And we call it with this code:
int jellyDonuts;
getOrder(jellyDonuts);
Copyright © 2012 Pearson Education, Inc.
Something Like Pointers:
Reference Variables
The donuts parameter, in the getOrder function,
points to the jellyDonuts variable.
C++ automatically stores
the address of
jellyDonuts in the
donuts parameter.
Copyright © 2012 Pearson Education, Inc.
Pointer Variables
• Pointer variables are yet another way using a
memory address to work with a piece of data.
• Pointers are more "low-level" than arrays and
reference variables.
• This means you are responsible for finding the
address you want to store in the pointer and
correctly using it.
Copyright © 2012 Pearson Education, Inc.
Pointer Variables
• Definition:
int *intptr;
• Read as:
“intptr can hold the address of an int”
• Spacing in definition does not matter:
int * intptr; // same as above
int* intptr; // same as above
Copyright © 2012 Pearson Education, Inc.
Pointer Variables
• Assigning an address to a pointer variable:
int *intptr;
intptr = #
• Memory layout:
num intptr
25 0x4a00
address of num: 0x4a00
Copyright © 2012 Pearson Education, Inc.
Pointer Variables
• Each pointer variable must be preceded by
an asterisk (*)
• Example: double*x, *y;
– Indicates that x is a pointer to double
value.
– Indicates that y is a pointer to double
value.
• Example: double *x, y;
– Indicates that x is a pointer to double
value.
– Indicates that y is a double value.
(C) Copyright 2005-2006 by Dr.Abeer El_korany
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
The Indirection Operator
• The indirection operator (*) dereferences
a pointer.
• It allows you to access the item that the
pointer points to.
int x = 25;
int *intptr = &x;
cout << *intptr << endl;
This prints 25.
Copyright © 2012 Pearson Education, Inc.
The Indirection Operator
• A variable contains a specific value.
• Example:
int count;
count = 7;
• But…A pointer variable can contain a
memory address as its value.
• Example:
int *countPtr;
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
The Indirection Operator
• A variable name directly references a
value.
• A pointer indirectly references a value.
• Referencing a value through a pointer is
called indirection.
(C) Copyright 2005-2006 by Dr.Abeer El_korany
Copyright © 2012 Pearson Education, Inc.
Indirection?
count
count directly references a
variable that contains the value 7 7
countPtr count
Pointer countPtr
indirectly references 7
a variable that
contains the value 7
Copyright © 2016 Soha Makady. All rights reserved.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
9-23
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Pointer Operators Precedence
• For the two pointer operators * and &,
they are considered as unary operators.
• Their associativity is right to left.
• Let us see an example
Copyright © 2016
Copyright © 2012 Pearson Soha
Education,Makady.
Inc. All rights reserved.
Pointer Operators
26
Copyright © 2012 Pearson Education, Inc.
Pointer Operators
27
Copyright © 2016
Copyright © 2012 Copyright
Pearson Soha
Education,©
Makady.
2016 Soha
Inc. All rights
Makady.
reserved.
All rights reserved.
9.3
The Relationship Between Arrays
and Pointers
Copyright © 2012 Pearson Education, Inc.
The Relationship Between
Arrays and Pointers
• Array name is starting address of array
int vals[] = {4, 7, 11};
4 7 11
starting address of vals: 0x4a00
cout << vals; // displays
// 0x4a00
cout << vals[0]; // displays 4
Copyright © 2012 Pearson Education, Inc.
The Relationship Between
Arrays and Pointers
• Array name can be used as a pointer
constant:
int vals[] = {4, 7, 11};
cout << *vals; // displays 4
• Pointer can be used as an array name:
int *valptr = vals;
cout << valptr[1]; // displays 7
Copyright © 2012 Pearson Education, Inc.
9-31
Copyright © 2012 Pearson Education, Inc.
9.4
Pointer Arithmetic
Copyright © 2012 Pearson Education, Inc.
Pointer Arithmetic
• Operations on pointer variables:
Operation Example
int vals[]={4,7,11};
int *valptr = vals;
++, -- valptr++; // points at 7
valptr--; // now points at 4
+, - (pointer and int) cout << *(valptr + 2); // 11
+=, -= (pointer valptr = vals; // points at 4
and int) valptr += 2; // points at 11
- (pointer from pointer) cout << valptr–val; // difference
//(number of ints) between valptr
// and val
9-37
Copyright © 2012 Pearson Education, Inc.
Pointer Arithmetic Example
• int v[5] array has been declared, and its first
element is at memory location 3000.
• Assume that pointer vPtr has been initialized to
point to v[0] (i.e., the value of vPtr is 3000).
• How could such initialization be done?
• int *vPtr = v;
• int *vPtr =
&v[0];
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointer Arithmetic Example(Cont’d)
• In normal arithmetic 3000 + 2 would result in 3002.
• In pointer arithmetic vPtr = vPtr + 2 has a different
result!
– vPtr = vPtr + 2 vPtr = 3000 + 2 *
size_of_object_dataType.
– The data type is the one the pointer points to
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointer Arithmetic Example(Cont’d)
• If vPtr is an int pointer, and int object takes 4 bytes
of memory.
– vPtr = 3000 + 2*4 = 3008 vPtr points to v[2]
• What if int object takes 2 bytes of memory?
– vPtr = 3000 + 2*2 = 3004
• What if it is an array of float elements?
– Based
Copyright ©on the Education,
2012 Pearson size Inc.
of float.
Copyright © 2016 Soha Makady. All rights reserv
Pointer Arithmetic Example(Cont’d)
• Assume that we incremented vPtr to point to 3016.
• What would this statement do: vPtr -=4;
– It would set vPtr to point to 3000;
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointer Arithmetic
• How about those two statements?
++vPtr;
vPtr ++;
Each of them increments the pointer to point to
the next element of the array.
• Example:
int a[] = {1, 2, 3, 4, 5};
int *aPtr = a;
cout<<"*(aPtr++)"<<*(aPtr++); //prints 1
cout<<"*(++aPtr)"<<*(++aPtr); //prints 3
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointer Arithmetic
• How about those two statements?
--vPtr;
vPtr--;
Each of them decrements the pointer to point to
the previous element of the array.
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointer Arithmetic
• Pointer variables pointing to the same array can
be subtracted from one another.
• Example:
int v[5];
int *vPtr = &v[0];
int*vPtr2 = &v[2];
int m = vPtr2 - vPtr;
cout<<"\n m = "<<m; //prints 2 (the number of
array elements from vPtr to vPtr2)
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointers and Arrays
• An array name can be thought of as a pointer to the
first element of the array.
• Example:
– int b[5];
– int *bPtr = b;
– bPtr = b; //assigns the address of array b to bPtr
– bPtr = &b[0]; //assigns address of array b to bPtr
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointers and Arrays
• How about: * ( bPtr + 3)
– It is equivalent to b[3]
– This notation is called pointer/offset notation.
3 is the offset.
• What if we remove the parentheses?
– * bPtr + 3
– * has higher precedence than +
– Would add 3 to *bPtr value
• What would &b[3] mean?
– The address of element with index 3 in the
array.
– It is equivalent to bPtr + 3
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
Pointers and Arrays
• How about: * ( b + 3)
– It is equivalent to b[3]
• Pointers can be subscripted exactly as arrays
can.
– E.g. bPtr[1] refers to the array element b[1]
– This notation is called pointer/subscript notation.
• How about b += 3
– Causes a compilation error
– It tries to modify a constant pointer that always points
to the beginning of the array.
Copyright
Copyright © 2012 Pearson Education,©Inc.
2016 Soha Makady. All rights reserved.
9.5
Initializing Pointers
Copyright © 2012 Pearson Education, Inc.
Initializing Pointers
• Can initialize at definition time:
int num, *numptr = #
int val[3], *valptr = val;
• Cannot mix data types:
double cost;
int *ptr = &cost; // won’t work
• Can test for an invalid address for ptr with:
if (!ptr) ...
Copyright © 2012 Pearson Education, Inc.
9.6
Comparing Pointers
Copyright © 2012 Pearson Education, Inc.
Comparing Pointers
• Relational operators (<, >=, etc.) can be
used to compare addresses in pointers
• Comparing addresses in pointers is not
the same as comparing contents pointed
at by pointers:
if (ptr1 == ptr2) // compares
// addresses
if (*ptr1 == *ptr2) // compares
// contents
9-53
Copyright © 2012 Pearson Education, Inc.
Exercise
What is the output?
Copyright © 2012 Pearson Education, Inc.