csc 107 – programming for science. today’s goal when lecture over, start understanding pointers...
TRANSCRIPT
LECTURE 26: POINTERS
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 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
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
Setting a Variable
Consider following code:
int x, y;
x = 5;
y = x;
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;
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
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
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
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;
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;
Setting a Pointer
Consider following code:
int x, *y;
x = 5;
y = &x;
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;
& 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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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);
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!
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!
Your Turn
Get into your groups and try this assignment
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!