290 likes | 390 Vues
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
E N D
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 • Pointers still hard & this is hardest topic of term
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
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 unknown int*jane, *dick;char*itsASecret;float*pointer;
& 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 set double x, *y = &x;int *a, *b = &a;float *c = a,d = *a;
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Everyone's Favorite Game Show • Make some noise to play: Value or Address double 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
Array Kindais a Pointer • Given the following code: inty;int x[10];x[4] = 5;y = x[0];
Array Kindais a Pointer • We really mean: // Get a memory location & name it yinty;// 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];
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)
Arrays vs. Pointers Arrays Pointers Yams Sweet Potatoes
Arrays vs. Pointers Arrays = Yams Pointers = 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
Arrays vs. Pointers Arrays = Yams Pointers = 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
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;
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
Arrays + Pointers = Arrays Pointers Yams Sweet Potatoes
Your Turn • Get into your groups and try this assignment
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.