Pointers 10/5/07 Pointers 1
10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it.
10/5/07 Pointers 3 Variable For example 1:#include <stdio.h> 2:int main() 3:{ 4: float fl=3.14; 5: printf("%.2f\n", fl); 6: return 0; 7:}
Variable At line (4) in the program above, the computer reserves memory for fl. In our examples, we'll assume that a float requires 4 bytes. Depending on the computer's architecture, a float may require 2, 4, 8 or some other number of bytes. When fl is used in line (5), two distinct steps occur: o o The program finds and grabs the address reserved for fl. In this example 924. The contents stored at that address are retrieved. To generalize, whenever any variable is accessed, the above two distinct steps occur to retrieve the contents of the variable. 10/5/07 Pointers 4
10/5/07 Pointers 5 More examples 1: #include <stdio.h> 2: int main() 3: { 4: float fl=3.14; 5: printf("fl's address=%u\n", (unsigned int) &fl); 6: return 0; 7: } This Code prints out the address of fl variable.
10/5/07 Pointers 6 Pointers Pointers are one of the most powerful features of C Pointers give programmers more control over the computer s memory A pointer is the memory address of a variable How are pointer variables declared in C?
10/5/07 Pointers 7 Pointer Declarations The memory address of a variable can be stored in another variable called a pointer Pointers are declared using the * operator The following declares a pointer to an integer o int *px; In the following statement, x is an integer and px is a pointer to an integer o int *px, x;
10/5/07 Pointers 8 Address Operator How do we assign to a pointer the address of a variable? Use the address operator (&) & returns the memory address of it s operand Example: o px = &x; Where have we used & before?
10/5/07 Pointers 9 Address Operator Operand of the address operator must be an lvalue An lvalue is something to which a value can be assigned Address operator cannot be applied to constants o int const NUM = 98; o px = &NUM; // ERROR o px = &8; // ERROR
10/5/07 Pointers 10 Pointer Operations int x, *px; x = 8; // set x to a value of 8 px = &x; // set the pointer variable to point // to the address of x printf( x is : %d \n,x); printf("address of x is: %u,px ); printf("address of x is: %u,& x);
10/5/07 Pointers 11 Indirection Operator How can we use the pointer variable to modify the value in the variable? o i.e. how to use px to change the value of x Answer: use the indirection operator (*) The * operator returns a synonym to whatever the pointer variable is pointing to Using the example on the previous slide o printf("px is pointing to: %d \n,*px);
10/5/07 Pointers 12 Indirection Operator Using * as we did in the previous example is called dereferencing the pointer Using our example, how can we dereference px so that it changes the value of x from 8 to 10? How can we change the value of x to a value entered by the user using the indirection operator?
10/5/07 Pointers 13 Common Pointer Mistakes What is wrong with the following? int x, *px; x = 8; *px = 2; px = 9; *x = 4;
10/5/07 Pointers 14 Pointers and Functions Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to the function swap. This is known as "call by value".
10/5/07 Pointers 15 Pointers and Functions If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. Lets look at couple of example.
10/5/07 Pointers 16 Passing by reference void square3 (int *pnum) { *pnum *= *pnum; } What would a function call to the above function look like?
10/5/07 Pointers 17 Function Call intval = 5; square3( &intval ); printf( %d,intval);
10/5/07 Pointers 18 Arithmetic and Logical Operations on Pointers A pointer may be incremented or decremented An integer may be added to or subtracted from a pointer. Pointer variables may be subtracted from one another. Pointer can used to perform arithmetic operation on the value its pointing to. Pointer variables can be used in comparisons, but usually only in a comparison to NULL.
Arithmetic Operations on Pointers 10/5/07 Pointers 19 When an integer is added to or subtracted from a pointer, the new pointer value is changed by the integer times the number of bytes in the data variable the pointer is pointing to. For example, if the pointer valptr contains the address of a double precision variable and that address is 234567870, then the statement: valptr = valptr + 2; would change valptr to 234567886
Arithmetic Operations on Pointers 10/5/07 Pointers 20 If you want use a pointer to perform arithmetic operations on a value it pointing then you have to deference it. int x, * ptrx; x = 8; ptrx = &x; ptrx = *ptrx + 1; (value of x=9) ptrx = *ptrx * 2; (value of x=18)
10/5/07 Pointers 21 Precedence Higher (assiociate left to right): (), [], {} Lower (right to left): ++, --, *, & Even lower (left to right): +, -