lecture 28: pointers versus arrays
DESCRIPTION
CSC 107 – Programming For Science. Lecture 28: Pointers VERSUS ARRAYS. Today’s Goal. Learn relationship between pointers & arrays How and why they both are similar Take advantage of relationship with built-in functions Understand what NULL is & why we use NULL - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/1.jpg)
LECTURE 28: POINTERS VERSUS ARRAYS
CSC 107 – Programming For Science
![Page 2: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/2.jpg)
Today’s Goal
Learn relationship between pointers & arrays How and why they both are similar Take advantage of relationship with built-in
functions Understand what NULL is & why we use
NULL
Pointers still hard & this is hardest topic of term
![Page 3: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/3.jpg)
Pointers & Variables
Variable names a memory location Initial value unknown if it is not set Memory location updated via assignment
Pointer is variable like any other… … but the pointer’s value is memory
location Aliases other variables when storing their
address Pointers can be used like other
variables… …but value is address and not a useful
number
![Page 4: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/4.jpg)
Declaring an Pointer
Must declare pointer before use This should not be surprise, just like any
variable Type & name required (as with all
declarations) As with any other variable, typical name
rules apply Include asterisk before name within
declaration Variable is now a pointer to requested
type Initial value is unknownint *jane, *dick;char *itsASecret;float* pointer;
![Page 5: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/5.jpg)
& and * Operators
& operator gets address of scalar variable Used only with variables, including array
elements Types must match, no automatic promotion
possible Pointers to pointers okay, but needs to be
type** Pointer used two ways: arrow & target
of arrow When code has the name alone, using the
arrow With *pointer, value at target can be used
or setdouble x, *y = &x;int *a, *b = &a;float *c = a, d = *a;
![Page 6: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/6.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 7: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/7.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 8: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/8.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 9: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/9.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 10: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/10.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 11: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/11.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 12: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/12.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 13: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/13.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 14: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/14.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 15: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/15.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 16: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/16.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 17: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/17.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 18: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/18.jpg)
Everyone's Favorite Game Show Make some noise to play: VALUE OR
ADDRESSdouble dX, dY, dZ, dA, dB, dD, bob[100];double *pX, *pY, *pZ, *pA, *pB, *babaGanoush;&dXpX*pYpow(*pY, 2)&(bob[50])*pA + 4*(&dA) * *pYbabaGanoush++&dA + 43bob
![Page 19: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/19.jpg)
Array Kinda is a Pointer
Given the following code:
int y;
int x[10];
x[4] = 5;
y = x[0];
![Page 20: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/20.jpg)
Array Kinda is a Pointer
We really mean:// Get a memory location & name it yint y;// Get 10 memory locations & // store address of first location in xint x[10];// Get address in x, skip past 4 locations, & store a 5x[4] = 5;// Get address in x, skip over 0 locations, load value &// store it in the location named yy = x[0];
![Page 21: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/21.jpg)
Pointers versus Arrays
Both types of variables store an address Can be assigned to one another if types
match To access value, can either use * or [index] *p same as p[0] - they are "synonyms" in
C++ Arithmetic works similarly - *(p+5) same as p[5]
Do not get carried away exploiting this idea Unlike arrays, memory not reserved for
pointer Arrays not used to alias other variables
(usually)
![Page 22: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/22.jpg)
Arrays vs. Pointers
Arrays Pointers
Yams Sweet Potatoes
![Page 23: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/23.jpg)
Arrays vs. Pointers
Arrays = YamsPointers = Sweet Potatoes
Makes space at declaration
Variable value is address
Use [] to access entries Can also access using *
Can be assigned a pointer
Needs target to be useful
Variable value is address
Use * to access target Can also access using []
Can be assigned array
![Page 24: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/24.jpg)
Arrays vs. Pointers
Arrays = YamsPointers = Sweet Potatoes
Makes space at declaration
Variable value is address
Use [] to access entries Can also access using *
Can be assigned a pointer
Needs target to be useful
Variable value is address
Use * to access target Can also access using []
Can be assigned array
Often usepointers & arrays interchangeably
![Page 25: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/25.jpg)
Pointers and Arrays
char x[5] = "pots"; char *y = &(x[1]);cout << x << " " << y << endl;*y = ‘a’;*x = ‘c’;cout << x << " " << y << endl;y = x;y[2] = ‘p’;x[1] = ‘o’;cout << x << " " << y << endl;*(y+2) = 'a';*(x+3) = 't';cout << x << " " << y << endl;
![Page 26: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/26.jpg)
Arrays + Pointers =
Pointer arithmetic enables mixing & matching Relies on fact that * and [] are synonyms Used processing cStrings (& with other
arrays, too) Provides another way to access array
elements Use foo[nnn] to access entry in foo at
index nnn But could also access entry using *(foo+nnn)
Pointers follow same rules and work similarly But, pointers access offset from where they
point
![Page 27: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/27.jpg)
Arrays + Pointers =
Arrays Pointers
Yams Sweet Potatoes
![Page 28: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/28.jpg)
Your Turn
Get into your groups and try this assignment
![Page 29: Lecture 28: Pointers VERSUS ARRAYS](https://reader033.vdocument.in/reader033/viewer/2022051417/56814e02550346895dbb6ebd/html5/thumbnails/29.jpg)
For Next Lecture
cStrings & pointers discussed in Section 12.7 How can we take advantage of the
similarities? What other functions are built-in to C++? Real world uses of pointers & pointer
arithmetic?
Angel also has Program Assignment #2 due Fri.