CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 6: Pointers
Pointers Pointers are references to other things Pointers are the address of some other variable in memory things can be data (i.e. int s, char s, double s) or other pointers A pointer of any type is a 32-bit number which should be interpreted as an address Pointer variables store a location of something stored elsewhere Rather than refer to a value directly, we can refer to it via its address 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 107 20bd4 43 20bd8 00 20bdc 00 20be0 00 Addresses in Hex
Pointers in everyday life The concept of a pointer is very common and used in many places in everyday life Phone numbers, e-mail or mailing addresses are references or pointers to you or where you live Excel workbook has cell names we can use to reference the data ( =$A$1 means get data in $A$1) URL s (www.usc.edu is a pointer to a physical HTML file) and can be used in any other page to point to USC s website
Why use Pointers? Share access to common data (hold onto one copy, everybody points to it) Flexibility (dynamic data structures) Precisely control of memory allocation/ deallocation ourselves
5 Declaring Pointer Variables Syntax: Examples: int *p; char *ch; These statements are equivalent: int *p; int* p; int * p; C++ Programming: Program Design Including Data Structures, Sixth Edition
6 Declaring Pointer Variables In the statement: int* p, q; Only p is a pointer variable q is an int variable To avoid confusion, abach the character * to the variable name: int *p, q; int *p, *q; C++ Programming: Program Design Including Data Structures, Sixth Edition
7 Address of Operator (&) Address of operator (&): A unary operator that returns the address of its operand Example: int x; int *p; p = &x; Assigns the address of x to p C++ Programming: Program Design Including Data Structures, Sixth Edition
Pointers & operator yields address of a variable in C++ (Tip: Read &foo as address of foo ) int x = 1298; char y= a ; float z = 5.375; int dat[2] = {10,5}; &x =>??, &y =>??, &z =>??, &dat[1] =??; dat =>?? 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 10 5 00 00 00 x y z dat[0] dat[1]
Pointers & operator yields address of a variable in C++ (Tip: Read &foo as address of foo ) int x = 1298; char y= a ; float z = 5.375; int dat[2] = {10,5}; &x => 0x20bc4, &y => 0x20bc8, &z => 0x20bcc, &dat[1] => 0x20bd4; dat => 0x20bd0 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 10 5 00 00 00 x y z dat[0] dat[1] Number of bits used for an address depends on OS, etc. 32-bit OS => 32-bit addresses 64-bit OS => 64-bit addresses
10 Dereferencing Operator (*) Dereferencing operator (or indirection operator): When used as a unary operator, * refers to object to which its operand points Example: cout << *p << endl; Prints the value stored in the memory location pointed to by p C++ Programming: Program Design Including Data Structures, Sixth Edition
Pointers A pointer variable is a location in memory that is dedicated to storing an address to a certain type of data Variable means it can be changedat first it can hold the address of data item 1 then it can be updated to hold the address of data item 2 Requires 4-bytes of storage in a 32-bit system or 8-bytes in a 64-bit systems Use a * after the type to indicate this a pointer variable to that type of data Declare variables: 20bc4 1298 x int x = 1298; char y= a ; 20bc8 a y double z = 5.375; 20bcc 5.375 z int dat[2] = {10,5}; 20bd0 10 dat[0] int *ptr1; 20bd4 5 dat[1] ptr1 = &x; 20bd8 ptr1 ptr1 = &dat[0]; 20bdc ptr2 float *ptr2; 20be0 00 ptr2 = &z;
Pointers A pointer variable is a location in memory that is dedicated to storing an address to a certain type of data Variable means it can be changedat first it can hold the address of data item 1 then it can be updated to hold the address of data item 2 Requires 4-bytes of storage in a 32-bit system or 8-bytes in a 64-bit systems Use a * after the type to indicate this a pointer variable to that type of data Declare variables: int x = 1298; char y= a ; double z = 5.375; int dat[2] = {107,43}; int *ptr1; ptr1 = &x; ptr1 = &dat[0]; float *ptr2; ptr2 = &z; // ptr1 = 0x20bc4 // ptr1 = 0x20bd0 // ptr2 = 0x20bcc 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 107 43 20bc4 20bd0 20bcc 00 x y z dat[0] dat[1] ptr1 ptr2
De-referencing / Indirection Once a pointer has been written with an address of some other object, we can use it to access that object (i.e. dereference the pointer) using the * operator Read *foo as value pointed to by foo value at the address stored in foo (not value of foo or value of address of foo ) Using URL analogy, using the * operator on a pointer is like clicking on a URL Examples: int a = 5; a = a + *ptr1; (*ptr1)++; *ptr2 = *ptr1 - *ptr2; ; 20bc4 20bc8 20bcc 20bd0 20bd4 20bd8 20bdc 20be0 1298 a 5.375 10 5 20bd0 20bcc 00 x y z dat[0] dat[1] ptr1 ptr2
De-referencing / Indirection Once a pointer has been written with an address of some other object, we can use it to access that object (i.e. dereference the pointer) using the * operator Read *foo as value pointed to by foo value at the address stored in foo (not value of foo or value of address of foo ) Using URL analogy, using the * operator on a pointer is like clicking on a URL Examples: int a = 5; a = a + *ptr1; // a = 15 after exec. (*ptr1)++; // dat[0] = 11 *ptr2 = *ptr1 - *ptr2; // z = 11 5.375 = 5.625 * in a type declaration = declare/allocate a pointer * in an expression/assignment = dereference 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 10 20bd4 5 20bd8 20bd0 20bdc 20bcc 20be0 00 x y z dat[0] dat[1] ptr1 ptr2
Declaring vs Dereferencing Declaring a pointer int *ptr1; float *ptr2 = &z; Assignment ptr1 = dat; Dereferencing a pointer int a = 5; "a = a + *ptr1;" 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 10 20bd4 5 20bd8 20bd0 20bdc 20bcc 20be0 00 x y z dat[0] dat[1] ptr1 ptr2
Pointer Arithmetic Pointers are variables storing addresses Addresses are just numbers We can perform addition or subtraction on those pointer variables (i.e. addresses) just like any other variable The number added/subtracted is implicitly multiplied by the size of the object int *ptr = &x; ptr = ptr + 1; // address in ptr was incremented by 4 Examples: (*ptr1)++; // dat[0] = 11 ptr1++; x=*ptr1++; // ptr1 now points at dat[1] // x = dat[1] = 5 then inc. ptr1 // to 0x20bd8 (*(ptr1-2))++; // dat[0] = 12 20bc4 1298 20bc8 a 20bcc 5.375 20bd0 11 20bd4 5 20bd8 20bd0 20bdc 20bcc 20be0 00 x y z dat[0] dat[1] ptr1 ptr2
17 OperaGons on Pointer Variables Pointer arithmegc can be very dangerous: Program can accidentally access memory locagons of other variables and change their content without warning Some systems might terminate the program with an appropriate error message Always exercise extra care when doing pointer arithmegc C++ Programming: Program Design Including Data Structures, Sixth Edition
Pointers to Pointers to Pointers can point to other pointers Essentially a chain of links Example int k,x[3] = {105, 107, 109}; int *myptr = x; int **ourptr = &myptr; k = *myptr; k = (**ourptr) + 1; k = *(*ourptr + 1); 20bc4 105 20bc8 107 20bcc 109 20bd0 20bd4 20bd8 00 20bdc 00 20be0 00 k X[0] X[1] X[2] myptr ourptr
Pointers to Pointers to Pointers can point to other pointers Essentially a chain of links Example int k,x[3] = {105, 107, 109}; int *myptr = x; int **ourptr = &myptr; k = *myptr; // k = 105 k = (**ourptr) + 1; // k = 106 k = *(*ourptr + 1); // k = 107 20bc4 105 20bc8 107 20bcc 109 20bd0 20bc4 20bd4 20bd0 20bd8 00 20bdc 00 20be0 00 k X[0] X[1] X[2] myptr ourptr