2.14: How can I dynamically allocate a multidimensional array?
It is usually best to allocate an array of pointers, and then initialize each pointer to a dynamically-allocated "row."  Here is a two-dimensional example: 


	int **array1 = (int **)malloc(nrows * sizeof(int *));
	for(i = 0; i < nrows; i++)
		array1[i] = (int *)malloc(ncolumns * sizeof(int));

(In "real" code, of course, malloc would be declared correctly, and each return value checked.) 

You can keep the array's contents contiguous, while making later reallocation of individual rows difficult, with a bit of explicit pointer arithmetic: 


	int **array2 = (int **)malloc(nrows * sizeof(int *));
	array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));
	for(i = 1; i < nrows; i++)
		array2[i] = array2[0] + i * ncolumns;

In either case, the elements of the dynamic array can be accessed with normal-looking array subscripts: array[i][j]. 

If the double indirection implied by the above schemes is for some reason unacceptable, you can simulate a two-dimensional array with a single, dynamically-allocated one-dimensional array: 


	   int *array3 = (int *)malloc(nrows * ncolumns * sizeof(int));

However, you must now perform subscript calculations manually, accessing the i,jth element with array3[i * ncolumns + j].  (A macro can hide the explicit calculation, but invoking it then requires parentheses and commas which don't look exactly like multidimensional array subscripts.) 

Finally, you can use pointers-to-arrays: 


	int (*array4)[NCOLUMNS] =
		(int (*)[NCOLUMNS])malloc(nrows * sizeof(*array4));

, but the syntax gets horrific and all but one dimension must be known at compile time. 

