Basic Pointers
CSCI 112: Programming in C
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
CSCI 112: Programming in C
int *intPointer; char *charPointer;
int myInt; // Declare an int variable int *myIntPointer; // 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
int x;
x Declare an int variable, and allocate a space in memory for it.
34
0x01 0x02 0xAE 0xAF
int x; x = 34;
x Assign 34 to x. The value 34 goes into the memory cell for x.
34
0x01 0x02 0xAE 0xAF
int x; x = 34; int *point;
x Declare an int pointer called point. Allocate space in memory for this variable. *point
34 0x01
0x01 0x02 0xAE 0xAF
int x; x = 34; int *point; point = &x;
x Assign the address of x to point. We can now think of *point as being a “link” to x *point
34 0x01
0x01 0x02 0xAE 0xAF
int x; x = 34; int *point; point = &x;
x Assign the address of x to point. We can now think of *point as being a “link” to x *point
*point is “pointing” to where x is stored!
int x = 6 * 7; int *p = &x; int z = *p;
In (1), we assign a value to an int variable x, then “point” the variable point to the address of x.
// 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.
// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point; 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!
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!
// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point;
34 0x01
0x01 0x02 0xAE 0xAF
int x; x = 34; int *point; point = &x; int *point2; point2 = point;
x point and point2 both reference the same address/variable (x) *point *point2
In (3), we declare a new int variable y, and assign the value “pointed to” by point to it.
// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point;
to)
retrieve the value of the variable being pointed to!
value of x to y (which is of the int type)
int y = *point;
34 0x01
0x01 0x02 0xAE 0xAF
int x; x = 34; int *point; point = &x; int y;
x point is an int pointer to x We declare the int variable y, and space is allocated in memory for it. *point y
34 34 0x01
0x01 0x02 0xAE 0xAF
int x; x = 34; int *point; point = &x; int y; y = *point;
x We dereference point, and assign its value to y. END RESULT: y == x *point y
int x; int *myPointer; // This next line assigns 34 to the variable myPointer points to // Equivalent to writing "x = 34;" *myPointer = 34;
int x = my_function();
function can write the result into that variable:
int x = my_function(); void increment_int(int *value) { *value = *value + 1; } int x = 6; increment_int(x); // x now equals 7