29
LECTURE 28: POINTERS VERSUS ARRAYS CSC 107 – Programming For Science

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

Embed Size (px)

Citation preview

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

LECTURE 28: POINTERS VERSUS ARRAYS

CSC 107 – Programming For Science

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

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

& 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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

Array Kinda is a Pointer

Given the following code:

int y;

int x[10];

x[4] = 5;

y = x[0];

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

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

Arrays vs. Pointers

Arrays Pointers

Yams Sweet Potatoes

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

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

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: CSC 107 – Programming For Science. Today’s Goal  Learn relationship between pointers & arrays  How and why they both are similar  Take advantage of

Arrays + Pointers =

Arrays Pointers

Yams Sweet Potatoes

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

Your Turn

Get into your groups and try this assignment

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

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.