Lec17_pointers_part1
Lec17_pointers_part1
Pointers
showValues(numbers, SIZE);
int x = 25;
int *intptr = &x;
cout << *intptr << endl;
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.
countPtr count
Pointer countPtr
indirectly references 7
a variable that
contains the value 7
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
4 7 11
starting address of vals: 0x4a00
cout << vals; // displays
// 0x4a00
cout << vals[0]; // displays 4
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]
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
9-53
Copyright © 2012 Pearson Education, Inc.
Exercise