Next: Static Initialisation of Pointer Arrays Up: Pointers Previous: Arrays of Pointers

Multidimensional arrays and pointers

We should think of multidimensional arrays in a different way in C:

A 2D array is really a 1D array, each of whose elements is itself an array

Hence

~a[n][m] notation.

Array elements are stored row by row.

When we pass a 2D array to a function we must specify the number of columns - the number of rows is irrelevant.

The reason for this is pointers again. C needs to know how many columns in order that it can jump from row to row in memory.

Considerint a[5][35] to be passed in a function:

We can do:

f(int a[][35]) {.....}

or even:

f(int (*a)[35]) {.....}

We need parenthesis (*a) since [] have a higher precedence than *

So:

int (*a)[35]; declares a pointer to an array of 35 ints.

int *a[35]; declares an array of 35 pointers to ints.

Now lets look at the (subtle) difference between pointers and arrays. Strings are a common application of this.

Consider:

char *name[10];

char Aname[10][20];

We can legally do name[3][4] and Aname[3][4] in C.

However

NOTE: If each pointer in name is set to point to a 20 element array then and only then will 200 chars be set aside (+ 10 elements).

The advantage of the latter is that each pointer can point to arrays be of different length.

Consider:

Fig. 2D Arrays and Arrays of Pointers


Dave.Marshall@cm.cf.ac.uk
Fri May 20 13:40:49 BST 1994