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 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 & y int 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 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 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
Setting a Variable We understand this as: // Get memory locations for x & y int 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 & y int x, *y; // Store 5 in x ’s memory location x = 5; // Lookup x ’s memory location and… // store it's address in y 's location y = &x;
& and * Operators & variable & 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 = 5 int *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 = 7 int *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
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 Due on Friday so you should have already started!