Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CSC 107 – Programming For Science

2 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

3 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

4 Setting a Variable  Consider following code: int x, y; x = 5; y = x;

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

6 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

7 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

8 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

9 Declaring an Pointer

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

11 Setting a Pointer  Consider following code: int x, *y; x = 5; y = &x;

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

13 & 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;

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

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

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

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

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

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

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

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

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

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

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

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

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

27 Your Turn  Get into your groups and try this assignment

28 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 Weekly Assignment #10 posted today  Due next week as is the normal case


Download ppt "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."

Similar presentations


Ads by Google