1 / 29

Lecture 28: Pointers VERSUS ARRAYS

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

kaida
Download Presentation

Lecture 28: Pointers VERSUS ARRAYS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 107 – Programming For Science Lecture 28: Pointers VERSUS ARRAYS

  2. 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

  3. 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

  4. 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;

  5. & 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;

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Array Kindais a Pointer • Given the following code: inty;int x[10];x[4] = 5;y = x[0];

  20. 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];

  21. 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)

  22. Arrays vs. Pointers Arrays Pointers Yams Sweet Potatoes

  23. 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

  24. 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

  25. 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;

  26. 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

  27. Arrays + Pointers = Arrays Pointers Yams Sweet Potatoes

  28. Your Turn • Get into your groups and try this assignment

  29. 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.

More Related