Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of.

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of."— Presentation transcript:

1 CSC 107 – Programming For Science

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 hardest topic of term  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

5 & and * Operators variable  & operator gets address of scalar variable  Used only with variables  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: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

7 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

8 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

9 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

10 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

11 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

12 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

13 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

14 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

15 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

16 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

17 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

18 Everyone's Favorite Game Show  Make some noise to play: V ALUE OR A DDRESS double dX, dY, dZ, dA, dB, dD, bob[100]; double *pX, *pY, *pZ, *pA, *pB, *babaGanoush; &dX pX *pY pow(*pY, 2) &(bob[50]) *pA + 4 *(&dA) * *pY babaGanoush++ &dA + 43 bob

19 Array Kinda is a Pointer  Given the following code: int y; int x[10]; x[4] = 5; y = x[0];

20 Array Kinda is a Pointer  We really mean: // Get a memory location & name it y int y; // Get 10 memory locations & // store address of first location in x int x[10]; // Get address in x, skip past 4 locations, & store a 5 x[4] = 5; // Get address in x, skip over 0 locations, load value & // store it in the location named y y = 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 ArraysPointers YamsSweet Potatoes

23 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

24 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 use pointers & 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 = ArraysPointers YamsSweet 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.


Download ppt "CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of."

Similar presentations


Ads by Google