Arrays vs pointers

A pointer can refer to an individual item or to an array of items. In fact, an array is just a way of creating both a list of items and a pointer to that list's first item in one declaration. The array and pointer concepts are more or less completely interchangeable in C.

This means that subscripts can be used with pointers, as if they were arrays held at the address of the pointer. As C does no array bound checking, this is completely unsafe. You may assign the address of a simple int variable to a pointer to int and than access locations following it as if they belonged to an array of ints, as follows.

void main()
{
   int i, * ip;
   ip = & i;
   ip[0] = 3;
   ip[1] = 2;
   ip[2] = 1;
}

On the other hand, an array can be used as if it was a pointer. We have seen that arrays are always passed by reference, even though no & is written, and this is why. The only difference is that the address of an array cannot be changed by assignment. Thus, the following would not be allowed:

void main()
{
   int i, ia[];
   ia = &i;
}

Next - Pointer arithmetic.

Back to Contents page.