![]() |
How can I make a pointer to an array?
I'm not sure if it's called an array, but it's like this:
array[5][1] (for example) |
![]() |
This one is from taken the FAQ of comp.lang.c :
2.10:
|
A:
int array[NROWS][NCOLUMNS]; f(array);the function's declaration should match: f(int a[][NCOLUMNS]) {...}or f(int (*ap)[NCOLUMNS]) {...} /* ap is a pointer to an array */In the first declaration, the compiler performs the usual implicit parameter rewriting of "array of array" to "pointer to array;" in the second form the pointer declaration is explicit. Since the called function does not allocate space for the array, it does not need to know the overall size, so the number of "rows," NROWS, can be omitted. The "shape" of the array is still important, so the "column" dimension NCOLUMNS (and, for 3- or more dimensional arrays, the intervening ones) must be included. If a function is already declared as accepting a pointer to a pointer, it is probably incorrect to pass a two-dimensional array directly to it. References: K&R I Sec. 5.10 p. 110; K&R II Sec. 5.9 p. 113. Hope that helps...Christian |