prototyping (oh no! not again??)
Peter Holzer
hp at vmars.tuwien.ac.at
Wed Nov 28 01:32:36 AEST 1990
gordon at osiris.cso.uiuc.edu (John Gordon) writes:
> Given that you are passing an n-dimensional array to a function, you
>*MUST* explicitly provide at least n-1 of the dimensions in the argument
>declaration.
> For example:
> if my_array is a 4-dimensional array, you *must* explicitly
>provide at least 3 of the dimensions when you pass my_array to a function.
> main()
> {
> int my_array[6][5][4][3];
> .
> .
> .
> }
> function_one(int array[6][5][4][])
> {
> .
> .
> .
> }
> Note: There is a rule that governs which dimension may be left out; I
>think it is one of the end ones, but I am not sure which one.
You may leave out the first dimension.
my_array is an array of six elements of type int [5][4][3], and
is treated in expressions like a pointer to an int [5][4][3].
Thus function_one wants a pointer to int [5][4][3]. Thus it
should be declared as function_one (int (* array)[5][4][3]).
The form function_one(int array [][5][4][3]) is equivalent, but
it looks as if array were an array, which it is not, so I
recommend the first form.
To answer the original posters question: The best method
(compromise between speed/portability/readability) is to set up
an array of pointers (to pointers ...) to items. You can then
create arrays of arbitrary shape and size at runtime. E.g.
#define N 20
#define M 30
main ()
{
int ** p;
int i;
if ((p = malloc (N * sizeof (* p))) == NULL) error ();
for (i = 0; i < N; i ++) {
if ((p[i] = malloc (M * sizeof (** p))) == NULL) error ();
}
function_one (p);
}
function_one (int ** array)
{
for (i = 0; i < N; i ++){
for (j = 0; j < M; j ++) {
/* do something with */ array [i][j];
}
}
}
Isn't that in the FAQ list?????
It should be there, because it is asked very often.
--
| _ | Peter J. Holzer | Think of it |
| |_|_) | Technical University Vienna | as evolution |
| | | | Dept. for Real-Time Systems | in action! |
| __/ | hp at vmars.tuwien.ac.at | Tony Rand |
More information about the Comp.lang.c
mailing list