C Pointers 55:017, Computers in z Powerful C feature but challenging to Engineering– C Pointers understand z Some uses of pointers include z CllbCall by re ference parame ter passage z Dynamic structures z Data structures that can shrink or grow z Creating arrays during program execution z Other examples include linked lists, stacks and trees

Indirection Creating Pointers z Indirection = referencing a value through a pointer z Pointers (like any other variables) must be declared int count = 7; /* Regular int variable */ before they can be used int *countPtr; /* Pointer to int */ z Examples countPtr = &count; /*Set countPtr to point to count */ int *countPtr,,; count; /* Can now use count and *countPtr interchangeably */ z countPtr is declared as type int * count = count +1; z int * means a pointer to an integer value /* is the same as */ z “countPtr is a pointer to an int” *countPtr = *countPtr +1; z “countPtr holds the address in memory of an integer value” z The “*” can be used to define a pointer to any C countPtr 7 count .

Pointer Declarations Pointer Declarations z Read pointer declarations from right to left and z General format for declaring a variable as a pointer to a particular type: substitute the word “address” for the * operator z name-of-type *nameOfPointer int * iPtr; z This declares nameOfPointer as a pointer int *iPtr; /* pointer to type int */ float *fPtr; iPtr is address of an integer char *cPtr; double *dPtr; int * iPtr; z What are iPtr, fPtr, cPtr, and dPtr? z Each is a pointer to its associated data type *iPtr is an integer

1 Pointer Declarations Initializing Pointers z Read pointer declarations from right to left and substitute the word “address” for the * operator z Pointers should be initialized when declared, or in an assignment statement int * * iPtr; z Initialization needed before they are used (just like other variables) iPtr is an address of an address z Pointer initialization values of an integer z NULL: Equivalent to zero, defined in stdio.h, *iPtr is an address of an integer z used to indicate a pointer to “nothing” **iPtr is an integer z A (usually of another variable) ***iPtr is a syntax error z or from a memory allocation function)

Pointer Operators Pointer vs. Addresses z Address operator, & z Pointer variables actually hold the memory address of the location that they “point to”. z Unary operator (needs only 1 operand) z Returns the address of an operand z Example 6000 7 memory address 6000 int y = 5; yptr y int *yPtr; /* Assigns the address of the variable y to the pointer variable yPtr */ z Memory addresses are assigned by the compiler so we generally don’t care about the specific address yPtr = &y; value stored in a pointer—we only need to know y what it “points to” yPtr

/* Using the & and * operators fig7_4.c */ Indirection Operator #include

main() { z The “*” that we see in expressions using int a; /* a is an integer */ pointers is the indirection operator int *aPtr; /* aPtr is a pointer to an integer */

z Also called the dereferencing operator a = 7; z aPtr = &a; /* aPtr set to address of a */ Returns the value of the data to which the prin tf("The a ddress o f a is %p\n" pointer is pointing "The value of aPtr is %p\n\n", &a, aPtr); printf("The value of a is %d\n" z Example: "The value of *aPtr is %d\n\n", a, *aPtr); printf(“Showing that * and & are complements of " printf("%d", *yPtr); "each other.\n“, "&*aPtr = %p\n“, /* prints 7 */ "*&aPtr = %p\n", Note: The %p format specifier allows us z Using “*” like this is called dereferencing a pointer &*aPtr, *&aPtr); return 0; to print an address as a hexadecimal } number

2 Output from Preceding Program Another Pointer Example

double flt1 = 100.0; The address of a is 7ffff030 The value of aPtr is 7ffff030 double *flt_ptr; flt_ptr = &flt1; The value of a is 7 The value of *aPtr is 7 z Note: fltflt_ptr ptr does not store a double value but rather a pointer to another variable (flt1) that Showing that * and & are complements of each other contains a double &*aPtr = 7ffff030 z Pointer is useless unless it’s pointing to something *&aPtr = 7ffff030 z Indirection with the * operator retrieves the value to which the pointer refers

Pointer Assignments Pointer Assignments Solution: Trace all variable at each line of code int *iptr, n=7, j=23, i=3; int *iptr, n=7, j=23, i=3; iptr = &i; iptr = &i; *iptr=i=3, n=7, j=23 n = *iptr; *iptr = j; n = *iptr; *iptr=i=3, n=3, j=23 *iptr = *iptr + n + 10; /* Trace: What is the value of *iptr? */ *iptr = j; *iptr=i=23, n=3, j=23

*iptr = *iptr + n + 10; *iptr=i=23+3+10=36, n=3, j=23

Pointer Assignments Pointer Assignments What is going on in the computer memory? What is going on in the computer memory? 1. int *iptr, n=7, j=23, i=3; 1. int *iptr, n=7, j=23, i=3;

2. iptr = &i; 2. iptr = &i; 3. n = *iptr; 3. n = *iptr; 4. *iptr = j; 4. *iptr = j; 5. *iptr = *iptr + n + 10; 5. *iptr = *iptr + n + 10; Result of line 1. Result of line 2. Result of line 3. Result of line 4. Address Value Address Value Address Value Address Value iptr ÅÆ 6000 unknown iptr ÅÆ 6000 6012 iptr ÅÆ 6000 6012 iptr ÅÆ 6000 6012 n ÅÆ 6004 7 n ÅÆ 6004 7 n ÅÆ 6004 3 n ÅÆ 6004 3 j ÅÆ 6008 23 j ÅÆ 6008 23 j ÅÆ 6008 23 j ÅÆ 6008 23 i ÅÆ 6012 3 *iptr, i ÅÆ 6012 3 *iptr, i ÅÆ 6012 3 *iptr, i ÅÆ 6012 23 6016 6016 6016 6016 6020 6020 6020 6020

3