Question 6.20

How can I use statically- and dynamically-allocated multidimensional arrays interchangeably when passing them to functions?


There is no single perfect method. Given the declarations

	int array[NROWS][NCOLUMNS];
	int **array1;		/* ragged */
	int **array2;		/* contiguous */
	int *array3;		/* "flattened" */
	int (*array4)[NCOLUMNS];
with the pointers initialized as in the code fragments in question 6.16, and functions declared as
	f1(int a[][NCOLUMNS], int nrows, int ncolumns);
	f2(int *aryp, int nrows, int ncolumns);
	f3(int **pp, int nrows, int ncolumns);
where f1 accepts a conventional two-dimensional array, f2 accepts a ``flattened'' two-dimensional array, and f3 accepts a pointer-to-pointer, simulated array (see also questions 6.18 and 6.19), the following calls should work as expected:
	f1(array, NROWS, NCOLUMNS);
	f1(array4, nrows, NCOLUMNS);

f2(&array[0][0], NROWS, NCOLUMNS); f2(*array, NROWS, NCOLUMNS); f2(*array2, nrows, ncolumns); f2(array3, nrows, ncolumns); f2(*array4, nrows, NCOLUMNS);
f3(array1, nrows, ncolumns); f3(array2, nrows, ncolumns);
The following two calls would probably work on most systems, but involve questionable casts, and work only if the dynamic ncolumns matches the static NCOLUMNS:
	f1((int (*)[NCOLUMNS])(*array2), nrows, ncolumns);
	f1((int (*)[NCOLUMNS])array3, nrows, ncolumns);

It must again be noted that passing &array[0][0] (or, equivalently, *array) to f2 is not strictly conforming; see question 6.19.

If you can understand why all of the above calls work and are written as they are, and if you understand why the combinations that are not listed would not work, then you have a very good understanding of arrays and pointers in C.

Rather than worrying about all of this, one approach to using multidimensional arrays of various sizes is to make them all dynamic, as in question 6.16. If there are no static multidimensional arrays--if all arrays are allocated like array1 or array2 in question 6.16--then all functions can be written like f3.


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback