Académique Documents
Professionnel Documents
Culture Documents
Pointers toArrays
Arrays andpointerscloselyrelated
Array namelikeconstantpointer
Pointerscandoarraysubscriptingoperations
261102
Computer Programming
Accessingarrayelementswithpointers
Elementb[n] canbeaccessedby*(bPtr + n)
Calledpointer/offsetnotation
Addresses
&b[3] sameasbPtr + 3
Lecture11:Pointers&Arrays
Value :Arraynamecanbetreatedaspointer
b[3] sameas *(b + 3)
Pointerscanbesubscripted(pointer/subscript
notation)
bPtr[3] sameasb[3]
Pointers toArrays
Apointervariablecanbeusedtoaccesstheelements
ofanarrayofthesametype.
Output
92 75 92 75 88 92 75
Pointers to2DArrays
int score[2][3] = {{92,85,75},{88,79,54}};
int *sPtr = score;
[Error]cannotconvert'int (*)[3]'to'int*'ininitialization
Arraystyle
int *sPtr = score; isequivalentto int *sPtr = &score[0];
Pointerstyle
Usearraynameas
pointername
NotethatthearraynamegradeList actslikethe
pointervariablemyGrades.
Inthiscase,sPtr++willmovetheaddressby4*3bytes
Pointers to2DArrays
Pointers to2DArrays
score[0][0]
score[0][1]
score[0][2]
int [3]
score[0]
actsasint *
score
actsasint **
Output
sPtr = 0x22fe20
sPtr+1 = 0x22fe2c
*sPtr = 0x22fe20
**sPtr = 92
*(sPtr+1) = 0x22fe2c
**(sPtr+1) = 88
int
score[1][0]
score[1][1]
score[1][2]
score[1]
Arraynameactslikeapointer
Pointers to2DArrays
sPtr
score[0]
score[0][0]
Arrayname
ofint [3]
int
*sPtr =valueofscore[0]
Score[0]isarraynamethatactsaspointertoscore[0][0]
Valueofscore[0]becomesaddressofscore[0][0]
*sPtr =addressofscore[0][0]
Pointers to2DArrays
sPtr
score[0]
score[0][0]
Arrayname
ofint [3]
int
**sPtr =*score[0]=valueofscore[0][0]
score[0]isarraynamethatactsaspointertoscore[0][0]
sPtr+1
score[1]
score[1][0]
Arrayname
ofint [3]
int
(Actaspointerstoresaddressofscore[1][0])
Pointers to2DArrays
10
Pointers to2DArrays
Usepointer
nameasarray
name
Usearray
nameas
pointername
Output
Output
sPtr[0][0] = 92
*sPtr[1] = 88
(*sPtr)[1] = 85
**score = 92
**(score+1) = 88
*(*score+1) = 85
**sPtr = 92
**(sPtr+1) = 88
*(*sPtr+1) = 85
*(*(sPtr+1)+2) = 54
11
Pointers to2DArrays
12
ArraysofPointers
ArraythatcontainsNpointers
type *name[N]
Output
sPtr = 0x22fe20
sPtr+1 = 0x22fe24
*sPtr = 92
*(sPtr+1) = 85
*(sPtr+4) = 79
Pointerthatpointstoarray
type (* name)[N]
13
ArraysofPointers
sPtr[0] =
sPtr[1] =
sPtr[2] =
sPtr[3] =
a = 1000
b = 500
c = 100
d = 50
14
ArraysofPointers
Output
0x22fe2c
0x22fe28
0x22fe24
0x22fe20
Output
name[0] = Luffy
name[1] = Zoro
(int*) name[0] = 0x488000
(int*) name[1] = 0x488006
*name[0] = L
*name[1] = Z
*(name[0]+1) = u
*(name[0]+2) = f
Similarto
stringname[]={"Luffy","Zoro","Nami","Usopp","Sanji"};
cout <<"name[0]="<<name[0]<<"\n";
cout <<"name[1]="<<name[1]<<"\n;
//Deferencing operator*cannotbeusedforC++stringobject
15
Example11A:DestinyDraw
16
Example11A:DestinyDraw
Passbypointer(pointtodeck[0][0])
Arrayofchar
Arrayof
char*
Ifdeck[row][col]isequalto0
setdeck[row][col] = i
dptr
dptr+1
Functioncalling(passaddressofdeck[0][0])
pointerdptr = &deck[0][0]
dptr+12
dptr+13
dptr+13+1
deck[0][0]
deck[0][1]
deck[0][2]
deck[0][12]
deck[1][0]
dect[1][1]
17
Example11A:DestinyDraw
18
Example11A:DestinyDraw
Passbyarray(deck)
Passbypointer
(pointtocount)
char *
Functioncall
char