basic pointers
play

Basic Pointers CSCI 112: Programming in C What the #$@# is a - PowerPoint PPT Presentation

Basic Pointers CSCI 112: Programming in C What the #$@# is a pointer? Pointers are variables Instead of holding values, they hold the address of another variable. They point to where that other variable is stored. Some new syntax A


  1. Basic Pointers CSCI 112: Programming in C

  2. What the #$@# is a pointer? Pointers are variables Instead of holding values, they hold the address of another variable. They “point” to where that other variable is stored.

  3. Some new syntax • A pointer variable is declared using a * • A pointer variable “points” to specific type • int pointer, char pointer, etc. • A pointer to an int : int *intPointer; • A pointer to a char : char *charPointer;

  4. What does a pointer do? • A pointer variable holds an address of another variable • Ex: an int* holds address of an int • We don’t assign a value to a pointer—we assign it an address!

  5. What does a pointer do? • A pointer variable holds an address of another variable • Ex: an int* holds address of an int • We don’t assign a value to a pointer—we assign it an address! • Remember the address of operator from scanf? (&) int myInt; // Declare an int variable int *myIntPointer; // Declare an int pointer variable myIntPointer = &myInt; // Point "myIntPointer" to the memory location of // "myInt"

  6. 0x01 0x02 0xAE 0xAF … We start out with a bunch of unallocated memory cells. The hexadecimal numbers above each cell represent the address.

  7. 0x01 0x02 0xAE 0xAF int x; … x Declare an int variable, and allocate a space in memory for it.

  8. 0x01 0x02 0xAE 0xAF int x; … 34 x = 34; x Assign 34 to x. The value 34 goes into the memory cell for x.

  9. 0x01 0x02 0xAE 0xAF int x; … 34 x = 34; x *point int *point; Declare an int pointer called point. Allocate space in memory for this variable.

  10. 0x01 0x02 0xAE 0xAF int x; x = 34; 34 … 0x01 int *point; x *point point = &x; Assign the address of x to point. We can now think of *point as being a “link” to x

  11. 0x01 0x02 0xAE 0xAF int x; x = 34; 34 … 0x01 int *point; x *point point = &x; Assign the address of x to point. We can now think of *point as being a “link” to x *point is “pointing” to where x is stored!

  12. Pointer operations: the “*” symbol • There are three uses for it: • Multiplication int x = 6 * 7; • Indicating that a variable is of a pointer type int *p = &x ; • Unary : “follows the pointer” (often called “dereferencing”) int z = *p ;

  13. Working with pointers // 1 int x = 34; int * point = & x ; // 2 int * point2 = point ; // 3 int y = * point ; In (1), we assign a value to an int variable x , then “point” the variable point to the address of x.

  14. Working with pointers // 1 int x = 34; int * point = & x ; // 2 int * point2 = point ; // 3 int y = * point ; In (1), we assign a value to an int variable x , then “point” the variable point to the address of x. Quick tip: to print the value of a pointer variable, use the “%p” Quick tip: to print the value of a pointer variable, use the “%p” format specifier! format specifier!

  15. Working with pointers // 1 int x = 34; int * point = & x ; // 2 int * point2 = point ; // 3 int y = * point ; In (2), we create another int pointer, point2 , and set it equal to point. This means *point and *point2 both “point” to the same place…x!

  16. A picture is worth 436 words 0x01 0x02 0xAE 0xAF int x; x = 34; … 34 0x01 int *point; x *point *point2 point = &x; point and point2 both reference the same address/variable (x) int *point2; point2 = point;

  17. Working with pointers // 1 int x = 34; int * point = & x ; // 2 int * point2 = point ; // 3 int y = * point ; In (3), we declare a new int variable y , and assign the value “pointed to” by point to it.

  18. Wait, what? • A pointer variable itself only holds an address (of the variable it points to) • But using the unary operator (*) we can “follow the pointer” and retrieve the value of the variable being pointed to! int y = * point ; • So if point is pointing to x, the above snippet is actually assigning the value of x to y (which is of the int type)

  19. A picture is worth 436 words 0x01 0x02 0xAE 0xAF int x; x = 34; … 34 0x01 int *point; x *point y point = &x; point is an int pointer to x int y; We declare the int variable y , and space is allocated in memory for it.

  20. A picture is worth 436 words 0x01 0x02 0xAE 0xAF int x; x = 34; … 34 34 0x01 int *point; x *point y point = &x; We dereference point , and assign its value to y. int y; END RESULT: y == x y = *point;

  21. Dereferencing works for assignment, too int x; int *myPointer; // This next line assigns 34 to the variable myPointer points to // Equivalent to writing "x = 34;" *myPointer = 34;

  22. Use case: output parameters • We’re used to functions returning a value int x = my_function();

  23. Use case: output parameters • We’re used to functions returning a value int x = my_function(); • We can also pass a pointer as an argument to function, and the function can write the result into that variable: void increment_int(int *value) { *value = *value + 1; } int x = 6; increment_int(x); // x now equals 7

Recommend


More recommend