Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help
next up previous contents
Next: realloc Up: More on Arrays Previous: More on Arrays   Contents

Multidimensional Arrays

The elements of aai[4][2] are stored in memory in the following order.

aai[0][0]aai[0][1]aai[1][0]aai[1][1]
aai[2][0]aai[2][1]aai[3][0]aai[3][1]

*aai is of type int[]. Note that:-

         aai[1][2] == *( (aai[1])+2) == *(*(aai+1)+2)
and that numerically
         aai == aai[0] == &aai[0][0]

*aai can be used as a pointer to the first element even though it is of type `array 4 of int' because it becomes `pointer to int' when used where a value is needed.

But *aai is not equivalent to a pointer. For example, you can't change its value. This distinction can easily and dangerously be blurred in multi-file situations illustrated in the following example. In

    extern int *foo;
foo is a variable of type pointer to int. foo's type is complete, (sizeof foo) is allowed. You can assign to foo. But given
    extern int baz[];
baz is a variable of type `array UNKNOWN-SIZE of int'. This is an `incomplete' type, you can't take (sizeof baz). You cannot assign to baz, and although baz will decay into a pointer in most contexts, it is not possible for (baz == NULL) ever to be true.

The compiler will allow you to mix the array/pointer notation and will get it right, but it needs to know what the reality is. Once you declare the array/pointer correctly, you can then access it either way.


next up previous contents
Next: realloc Up: More on Arrays Previous: More on Arrays   Contents
Tim Love 2010-04-27