Relationship Between Pointers and Arrays
Relationship Between Pointers and Arrays
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
3
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
• Define an array v[ 5 ] and a pointer vPtr 4
int A[5];
int B[10];
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
6
Example (2)
int b[5], *bPtr;
bPtr=b;
Is equivalent to:
bPtr=&b[0];
Example (3)
#include<iostream.h> xpointer=&z;
void main(){ cout<<*xpointer<<endl;//1
int x=1, z=1, xx[10]={10,20,30}; cout<<xpointer<<endl<<endl;
//0x0012FF78
int y[5]={11,22,33,44};
if(xx==&xx[0])
int *xpointer; cout<<"Array names are
xpointer=xx; constant pointers"<<endl;
cout<<*xpointer<<endl;//10 //the condition evaluates to //true
cout<<xpointer<<endl<<endl;
//0x0012FF50 xx=&y[0];
//error: attempt to change the
xpointer=&xx[0]; //address of a constant pointer
cout<<*xpointer<<endl;//10
cout<<xpointer<<endl<<endl; }
//0x0012FF50
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8
Accessing array elements with pointers
(pointer subscript notation)
The array name (b) is actually the address of
first element of the array b[ 5 ]
bPtr = &b[ 0 ]
bPtr = b;
bPtr[ 3 ] same as b[ 3 ]
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9
Accessing array elements with pointers
(pointer offset notation)
int b[5], *bPtr = b;
•The element b[2] can be alternatively referenced with the pointer
operation *(bPtr + 2), where 2 is the offset to the pointer. The
offset value is identical to the array subscript.
•When using pointers to reference the values of array elements as
in the above notation, it is referred to as the pointer/offset
notation.
b[ 0 ] same as *( b + 0 )
b[ 3 ] same as *( b + 3 )
Element b[ n ] can be accessed by *( b + n )
b[ 0 ] same as *( bPtr + 0 )
b[ 3 ] same as *( bPtr + 3 )
Element b[ n ] can be accessed by *( bPtr + n )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
10
• Pointer/offset notation
&b[ 0 ] same as bPtr + 0
&b[ 3 ] same as bPtr + 3
Pointer/offset notation where the pointer
is the array name
&b[ 0 ] same as b + 0
&b[ 3 ] same as b + 3
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12
Outline
Example (5)
b[3] //11
can be replaced with
*(bPtr+3) //*(&b[0]+3)
Or with
*(b+3) //*(&b[0]+3) is this equal to *b+3;?
*bPtr+3 //*(&b[0])+3=15
is equivalent to
b[0]+3 //12+3=15
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
14
*bPtr+3
is equivalent to
b[0]+3
*(bPtr+3)
is equivalent to
b[3]
&b[3]
is equivalent to
bPtr+3
And is equivalent to:
b+3
bPtr=&b[2];
cout<<*(bPtr+2); //prints b[4]
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15
Pointers Comparison
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
16
Example (1)
int *nPtr;
double *dPtr;
if(nPtr = = dPtr)
//error: not valid statement because the 2 pointers are of different data types
Example (2)
if (Ptr1==A)
cout<<“Ptr1 and A are same values”<<endl;
else
cout<<“Ptr1 and A are different values”<<endl;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
18
if (Ptr2 != Ptr3)
cout<<“Ptr2 and Ptr3 are different values”<<endl;
else
cout<<“Ptr2 and Ptr3 are same values”<<endl;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
19
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
20
Pointers Assignment
• Pointer variables can be assigned the values of other pointer
variables that are of the same data type
• If jPtr and iPtr are 2 pointers of the same data type, then the
result of the expression
jPtr = iPtr; is that the value of iPtr will be copied to jPtr so
that they both have the same memory location.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
21
11 i
22 11 j
*iPtr
... *jPtr = *iPtr;
0xbffffd20 iPtr
*jPtr
0xbffffd24 jPtr
Before the Assignment
Example:
int *ptr1;
void *ptr2;
ptr2 = ptr1;
ptr1 = (int *)ptr2;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
23
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24
Example
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25
10 A[0]
20 A[1]
30 A[2]
40 A[3]
50 A[4]
Ptr
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
26
10 A[0]
20 A[1]
30 A[2]
40 A[3]
50 A[4]
Ptr
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
27
10 A[0]
20 A[1]
30 A[2]
40 A[3]
50 A[4]
Ptr
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
28
Operator Precedence
Operators Associativity
Unary
+ - ++ -- ! * & (type) Right to Left
/ % Left to Right
+ - Left to Right
== != Left to Right
|| Left to Right
= += -= *= /= %= Right to Left
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
29
Addition on Pointers
• The expression Ptr + i results in a pointer to the ith
object beyond the object to which Ptr points.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
30
Example
int x=2;
int *Ptr = &x;
cout<<*(Ptr + 2)<<endl; //50
cout<<*Ptr<<endl; //2
Ptr+=2;
cout<<*Ptr; //50
...
...
Before adding 2 to the pointer
2 x
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
32
Example
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
33
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
34
Subtraction on Pointers
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Example 35
int x=2;
int *Ptr = &x;
cout<<*(Ptr - 2)<<endl;
cout<<*Ptr<<endl;
Ptr-=2;
cout<<*Ptr;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
37
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
39
• If vPtr contains the address 3000 and v2Ptr contains the address
3008, then the statement: x = v2Ptr – vPtr;
would assign to the variable x the number of array elements
from vPtr to v2Ptr. x equals 2 not 8.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
40
#include<iostream.h>
int sumArrayStandard(int [], int);
void sumArrayReference(int [ ], int, int *);
int sumArrPtrNotation(int *);
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
43
cout<<*sumPtr<<endl;
}
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
44
//using pointer notation
int sumArrPtrNotation(int *X){
int *xptr;
int sum=0;
return sum;
} © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
45
void main(){
int Y[5]={1,2,3,4,5};
int sum;
cout<<sumArrayStandard(Y,5)<<endl;
//displays 15
sumArrayReference(Y,5,&sum);
//displays 15
cout<<sumArrPtrNotation(Y)<<endl;
//displays 15
}//end of main
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
46
Example