Transcript
Page 1: 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

LECTURE 26: POINTERS

CSC 107 – Programming For Science

Page 2: 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

Today’s Goal

When lecture over, start understanding pointers What a pointer is and what it is not Why pointers are helpful & how pointers are

used

WARNING: Pointers are hard Ties together much of which we’ve done Getting comfortable & happy with them

takes a while

Page 3: 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

Variables

Variable name location to store data Memory location's initial value is unknown Assignments update memory location with

new value Memory location updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location

Variable can store only one value Use array otherwise so it can store 1 value

per entry

Page 4: 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

Setting a Variable

Consider following code:

int x, y;

x = 5;

y = x;

Page 5: 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

Setting a Variable

We understand this as:// Get memory locations for x & yint x, y;// Store 5 in x’s memory location x = 5;// Get value in x’s memory location and..// copy it into y’s memory location y = x;

Page 6: 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

x Also Known As…

Variable assignment only copies value Updating x does not change y Updating y does not change x

What if y used x’s memory location? x and y would then be aliases for same

location Changing x’s value changes y’s value Changing y’s value changes x’s value

Page 7: 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

Pointers

Pointer is another type of variable Stores address as pointer’s value With others, makes aliases for variables

Pointer variables are variables & must be declared Like all declarations, must include type and

name Add asterisk * before variable’s name

Once it is declared, can be used like any variable

Page 8: 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

Pointers

Pointer is another type of variable Stores address as pointer’s value With others, makes aliases for variables

Pointer variables are variables & must be declared Like all declarations, must include type and

name Add asterisk * before variable’s name

Once it is declared, can be used like any variable BUT address is value of this variable

Page 9: 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

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 * jennifer;char *pointerToChar;double* down;

Page 10: 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

Setting a Variable

We understand this as:// Get memory locations for x & yint x, y;// Store 5 in x’s memory location x = 5;// Get value in x’s memory location and..// copy it into y’s memory location y = x;

Page 11: 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

Setting a Pointer

Consider following code:

int x, *y;

x = 5;

y = &x;

Page 12: 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

Setting a Pointer

We understand this as:// Get 2 memory locations named x & yint x, *y;// Store 5 in x’s memory locationx = 5;// Lookup x’s memory location and…// store it's address in y's location y = &x;

Page 13: 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

& and * Operators

& operator gets the address of a variable Use with any variable, including array

elements Using the & operator with anything else

illegal Others, like literals & equations, do not

have address Follow 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;

Page 14: 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

Coding With Pointers

double 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;

Page 15: 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

Coding With Pointers

double 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;

Page 16: 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

Coding With Pointers

double 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;

Page 17: 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

Coding With Pointers

double 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;

Page 18: 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

Coding With Pointers

double 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;

Page 19: 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

Coding With Pointers

double 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;

Page 20: 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

Coding With Pointers

double 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;

Page 21: 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

Coding With Pointers

double 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;

Page 22: 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

Coding With Pointers

double 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;

Page 23: 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

Coding With Pointers

double 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;

Page 24: 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

Coding With Pointers

double 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;

Page 25: 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

Coding With Pointers

double 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;

Page 26: 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

Pointer Variables

int x = 7int *y = &x;int z = 10;cout << x << " " << y << " " << *y << " " << z << endl;

*y = 6;cout << x << " " << y << " " << *y << " " << z << endl;

x = 0;*y = *y + 3;cout << x << " " << y << " " << *y << " " << z << endl;

y = &z;cout << x << " " << y << " " << *y << " " << z << endl;

*y = 100;cout << x << " " << y << " " << *y << " " << z << endl;

y = 100;cout << x << " " << y << " " << *y << " " << z << endl;

Page 27: 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

Pass-By-Pointer Parameters

Pass-by-reference parameters similar in effect Similar rules of scoping and lifetime apply Creates “arrow” to argument when function

called For this, include * between type and

name Only where parameters listed, not in

arguments Spaces do not matter, so use what looks

best

void Mean(int a,int b, double* avg);double ReadIn(int * x, char * y);bool tooClose(double *sum, int tri);

Page 28: 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

Pass-By-Pointer Parameters

For code to compile argument must be variable Using argument's memory location, so this

required Since lack location to use, no literals or

expressions Data types must match exactly for it to

work Since parameter is pointer, must use *

for value Must use & in argument since we must

provide address!

Page 29: 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

Pass-By-Pointer Parameters

For code to compile argument must be variable Using argument's memory location, so this

required Since lack location to use, no literals or

expressions Data types must match exactly for it to

work Since parameter is pointer, must use *

for value Must use & in argument since we must

provide address!

Page 30: 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

Your Turn

Get into your groups and try this assignment

Page 31: 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

For Next Lecture

Arrays & pointers discussed in Sections 12.6 & 12.8 Both use arrows in a trace; are they related? How to exploit similarities in how each

operates Pointers & pointer arithmetic used with what

types?

Angel has Programming Assignment #2 available Due on Friday so you should have already

started!


Recommended