CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers ïƒ What a pointer is and what it is not ïƒ Why pointers

  • View
    213

  • Download
    1

Embed Size (px)

Text of CSC 107 – Programming For Science. Today’s Goal...

Computer Science 107

Lecture 26: PointersCSC 107 Programming For Science#Todays GoalWhen lecture over, start understanding pointersWhat a pointer is and what it is notWhy pointers are helpful & how pointers are used

WARNING: Pointers are hardTies together much of which weve doneGetting comfortable & happy with them takes a while2#VariablesVariable name location to store dataMemory location's initial value is unknownAssignments update memory location with new valueMemory location updated by assignment ONLYWhen variable is used in programuses current value at that memory locationVariable can store only one valueUse array otherwise so it can store 1 value per entry

3#Setting a VariableConsider following code:

int x, y;

x = 5;

y = x;

Setting a VariableWe understand this as:// Get memory locations for x & yint x, y;// Store 5 in xs memory location x = 5;// Get value in xs memory location and..// copy it into ys memory location y = x;

x Also Known AsVariable assignment only copies valueUpdating x does not change yUpdating y does not change xWhat if y used xs memory location?x and y would then be aliases for same locationChanging xs value changes ys valueChanging ys value changes xs value

6#PointersPointer is another type of variableStores address as pointers valueWith others, makes aliases for variablesPointer variables are variables & must be declaredLike all declarations, must include type and nameAdd asterisk * before variables nameOnce it is declared, can be used like any variable

7#PointersPointer is another type of variableStores address as pointers valueWith others, makes aliases for variablesPointer variables are variables & must be declaredLike all declarations, must include type and nameAdd asterisk * before variables nameOnce it is declared, can be used like any variableBUT address is value of this variable

8#Declaring an PointerMust declare pointer before useThis should not be surprise, just like any variableType & name required (as with all declarations)As with any other variable, typical name rules applyInclude asterisk before name within declarationVariable is now a pointer to requested type Initial value is unknownint * jennifer;char *pointerToChar;double* down; Setting a VariableWe understand this as:// Get memory locations for x & yint x, y;// Store 5 in xs memory location x = 5;// Get value in xs memory location and..// copy it into ys memory location y = x;

Setting a PointerConsider following code:

int x, *y;

x = 5;

y = &x;

11#Setting a PointerWe understand this as:// Get 2 memory locations named x & yint x, *y;// Store 5 in xs memory locationx = 5;// Lookup xs memory location and// store it's address in y's location y = &x;

12#& and * Operators& operator gets the address of a variableUse with any variable, including array elementsUsing the & operator with anything else illegalOthers, like literals & equations, do not have addressFollow pointer & get value at its address using *Only use with pointers, since it could be multiply int x = 5int *y = &x;int z = *y;float a, *b = &a, c = *b;

13#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;14#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;15#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;16#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;17#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;18#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;19#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;20#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;21#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;22#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;23#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;24#Coding With Pointersdouble x;double *y = &x;double *z = &(x+5);float a, *b;b = &a;y = x;y = &a;a = *b;b = &x;x = *b;x = *b + 5;a = *y;25#Pointer Variablesint x = 7int *y = &x;int z = 10;cout

View more >