next up previous
Next: September 18 Up: September 13 Previous: September 13


Pointers

C allows you to directly manipulate the byte address in main memory of data and program code. Every type (e.g. int, char, double) has a corresponding pointer type, declared

int *ip, k= 3;
ip= &k;
This declaration helps by suppressing the (often platform-dependent) details of the data type when doing pointer arithmetic. For example, if the declaration above set ip == 0xfff0, what should the value of ip + 1 be? This operation would be different for a character or long pointer.

When we want a function to change a variable (as a side effect), we generally pass the address of the variable.

void swap(int *a, int *b); 

/* some intervening code that uses swap */

void swap(int *a, int *b)
{
    int temp= *a;
    *a= *b;
    *b= temp;
}
Convince yourself that swap couldn't be implemented if its prototype were void swap(int a, int b).

There are three chapters in KN King ``C programming, A Modern Approach'' on pointers, and we'll be giving more information in tutorials.



Danny Heap 2002-12-16