of another variable as its value What are they used for? Generic access to several similar variables Call by reference function parameters Dynamically allocated data structures Creating copies of arrays and structures
also has a name, a value, and an address in memory on the runtime stack name value address number1 0xffbffa4c pointer1 0xffbffa48 NULL means that the pointer does not point to anything int *pointer1 = NULL; 5 NULL
pointer, the asterisk (*) indicates that we are creating a pointer, which is a variable that stores an address to another variable int *pointer1 = NULL;
pointer, the pointer’s type should match the variable’s type which it will point to So the type for number1 and pointer1 is int int number1 = 5; int *pointer1 = NULL;
(&) returns the address of number1 (0xffbffa4c), which is stored in pointer1 name value address number1 0xffbffa4c pointer1 0xffbffa48 Usually the “pointing” is represented with an arrow pointer1 = &number1; //0xffbffa4c 5 0xffbffa4c
value stored at the address that is stored in pointer1 name value address number1 0xffbffa4c pointer1 0xffbffa48 So the output is: 5 printf(“%i”, *pointer1); //5 5 0xffbffa4c
value which pointer1 points to, so number2 has value 5 name value address number1 0xffbffa4c pointer1 0xffbffa48 number2 0xffbffa44 number2 = *pointer1; //5 5 0xffbffa4c 5
dereferencing operator (*) if we want to access the address stored in the pointer name value address number1 0xffbffa4c pointer1 0xffbffa48 So the output is: 0xffbffa4c printf(“0x%p”, pointer1); //0xffbffa4c 5 0xffbffa4c
address operator (&) if we want to access the pointer’s address on the runtime stack name value address number1 0xffbffa4c pointer1 0xffbffa48 So the output is: 0xffbffa48 printf(“0x%p”, &pointer1);//0xffbffa48 5 0xffbffa4c
address operator (&) to access the local variable’s address on the runtime stack name value address number1 0xffbffa4c pointer1 0xffbffa48 So the output is: 0xffbffa4c printf(“0x%p”, &number1); //0xffbffa4c 5 0xffbffa4c
using number2 Note that %p is used to display addresses name value address number1 0xffbffa4c pointer1 0xffbffa48 number2 0xffbffa44 printf(“0x%p”, &number2); //0xffbffa44 5 0xffbffa4c 5
(*) a NULL pointer This will crash your program with this error message: Segmentation fault (core dumped) int *pointer1 = NULL; printf(“%i\n”, *pointer1);
when your program tries to access memory at an address that it is not supposed to have access to A core dump is a file that stores the complete state of a process (running program) that has crashed The term comes from magnetic-core memory, which was used as RAM in computers from 1955 to 1975
minute claymation video that also explains how pointers work! See the link on the class webpage: http://cslibrary.stanford.edu/ From the Stanford CS Education Library
different types to each other without a cast Add a float or double to a pointer Add two pointers Multiply, divide, do modulus, or shift a pointer
each line of executable code address name value 0xffbffa47 letter1 0xffbffa40 pointer1 char letter1 = 'A'; char *pointer1 = NULL; pointer1 = &letter1; ‘A’ 0xffbffa47
line of executable code address name value 0xffbffa47 letter1 0xffbffa40 pointer1 /*unary operators associate right to left, so dereference, then add*/ ++*pointer1; ‘B’ 0xffbffa47
add one address name value 0xffbffa47 letter1 0xffbffa40 pointer1 /*unary operators associate right to left, so have to use parentheses*/ (*pointer1)++; ‘C’ 0xffbffa47
do as you wish with a dereferenced pointer address name value 0xffbffa47 letter1 0xffbffa40 pointer1 0xffbffa3f letter2 *pointer1 = ((*pointer1 / 2) * 2) - 2; ‘C’ 0xffbffa3f ‘x’
of above code address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 double x=3.3, y=6.6, z=9.9; double *pointer1=&y; 3.3 6.6 9.9 0xffbffa38
because points in an double address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 pointer1 = pointer1 + 1; //WARNING: Don’t do this! 3.3 7.7 9.9 0xffbffa40
point to? address name value 0xffbffa40 x 0xffbffa38 y 0xffbffa30 z 0xffbffa2c pointer1 pointer1 = pointer1 - 2; //WARNING: Don’t do this! 4.4 7.7 9.9 0xffbffa30
This is showing what NOT to do. It is okay to add integers to or subtract integers from pointers, if they point to an array. However, it is not okay to change pointer values this way, if the pointer points to a single variable. It might point to the wrong variable, or point to random data.