Home Segments Index Top Previous Next

342: Mainline

Concretely, suppose that you define nptr to be a pointer variable that points to integers, and that you define number to be an array of integers:

int *nptr; 
int number[100]; 

Then, you can arrange for nptr's value to be the address of the first element in the array:

nptr = &number[0]; 

Once you have thus assigned a value to nptr, you can produce the address of other array elements by addition:

nptr + 0         /* Address of the first element */ 
nptr + 1         /* Address of the second element */ 
nptr + n - 1     /* Address of the nth element */ 

And once you know how to produce addresses, you know how to produce the elements themselves via the dereferencing operator:

*(nptr + 0)      /* Identifies the first element */ 
*(nptr + 1)      /* Identifies the second element */ 
*(nptr + n - 1)  /* Identifies the nth element */