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 pointer variable is declared using a * A pointer variable points to specific type intpointer, char pointer, etc. A pointer to an int: int *intpointer; A pointer to a char: char *charpointer;
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!
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; int *myintpointer; // Declare an int variable // Declare an int pointer variable myintpointer = &myint; // Point "myintpointer" to the memory location of // "myint"
0x01 0x02 0xAE 0xAF We start out with a bunch of unallocated memory cells. The hexadecimal numbers above each cell represent the address.
0x01 0x02 0xAE 0xAF x int x; Declare an int variable, and allocate a space in memory for it.
0x01 0x02 0xAE 0xAF 34 x int x; x = 34; Assign 34 to x. The value 34 goes into the memory cell for x.
0x01 0x02 0xAE 0xAF 34 x *point Declare an int pointer called point. Allocate space in memory for this variable. int x; x = 34; int *point;
0x01 0x02 0xAE 0xAF 34 0x01 x *point Assign the address of x to point. int x; x = 34; int *point; point = &x; We can now think of *point as being a link to x
0x01 0x02 0xAE 0xAF 34 0x01 x *point Assign the address of x to point. int x; x = 34; int *point; point = &x; We can now think of *point as being a link to x *point is pointing to where x is stored!
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;
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.
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 format specifier!
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!
A picture is worth 436 words 0x01 0x02 0xAE 0xAF 34 0x01 x *point2 *point point and point2 both reference the same address/variable (x) int x; x = 34; int *point; point = &x; int *point2; point2 = point;
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.
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)
A picture is worth 436 words 0x01 0x02 0xAE 0xAF 34 0x01 x y *point point is an int pointer to x We declare the int variable y, and space is allocated in memory for it. int x; x = 34; int *point; point = &x; int y;
A picture is worth 436 words 0x01 0x02 0xAE 0xAF 34 34 0x01 x y *point We dereference point, and assign its value to y. END RESULT: y == x int x; x = 34; int *point; point = &x; int y; y = *point;
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;
Use case: output parameters We re used to functions returning a value int x = my_function();
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