
passing 2d arrays by reference
Quote:
> i have some problems passing a 2d array of int's, by reference, to a
> subprogram.
> if i just pass a normal array of int's it works fine, but when i add
> on another set of square brackets, everything goes to hell :)
That's because there is not really such a thing as a 2D array, in C.
There are just arrays of arrays. Only the outermost array (the first
pair of []'s) may be unknown.
Quote:
> the following code gives me the error.. "arithmetic on pointer to an
> incomplete type", on the line calc() where i assign stuff to point[][]
It has to: it doesn't know how many elements there are in one 'row' of
your matrix, so it can't know where the second row would start.
Quote:
> void calc(int point[][]) {
> int i;
> for (i=0;i<2;i++)
> point[i][i] = (3 * point[i][i]);
> }
To make this work, you'ld have to make this
void calc (int point[][3]) {
/* rest can stay as is */
}
But this means you can only pass any array of dimension (something)x3
to this function. For fully dynamically-sized matrix operations, you
either need the "variable length array" extension of the C99 version
of Standard C, or resort to pointers and the allocation method shown
in the C FAQ.
--
Even if all the snow were burnt, ashes would remain.
--