# multi dimension arrays

Discussion in 'C Programming' started by Robert, Feb 18, 2004.

1. ### RobertGuest

Lets say you have an multi array of ints num[x][y] and send
it as a pointer to a function X(arg) have do a do this without
define the size?

It works with X(int (*num)[0]).
Why cant I use X(int **num) ?

//Robert

Robert, Feb 18, 2004

2. ### Russell HannekenGuest

"Robert" <> wrote in message
news...
> Lets say you have an multi array of ints num[x][y] and send
> it as a pointer to a function X(arg) have do a do this without
> define the size?
>
> It works with X(int (*num)[0]).
> Why cant I use X(int **num) ?

This is addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q6.18.html

You might also want to look at questions 6.19 and 6.20.

--
Russell Hanneken

Remove the 'g' from my address to send me mail.

Russell Hanneken, Feb 18, 2004

3. ### Peter PichlerGuest

"Robert" <> wrote in message
news...
> Lets say you have an multi array of ints num[x][y] and send
> it as a pointer to a function X(arg) have do a do this without
> define the size?

I find it difficult to parse your question. What language is it in?

> It works with X(int (*num)[0]).
> Why cant I use X(int **num) ?

Define "works". In function parameter context, the two declarations above
are equivalent. Unfortunately, they are both (most probably) wrong.
Consider:

int X (int (*num)[0]);

int main (void)
{
int x[3][4];
X(x);
return 0;
}

Your 'x' is an array of three arrays of four ints, organized in memory like
this:

x[0] x[1] x[2]
+-----------------+-----------------+-----------------+
| [0] [1] [2] [3] | [0] [1] [2] [3] | [0] [1] [2] [3] |
+-----------------+-----------------+-----------------+
^ ^ ^
x[0][0] x[1][0] x[1][3] ...etc.

To get from x[0][0] to x[1][0], you need to skip exactly four ints, i.e.
sizeof x[0] (or 4*sizeof(int) if you like) bytes.

But your function X expects an array of pointers to int. Array of
unspecified size of pointers to int, to be more precise. Like this:

x[0] x[1] x[2] ...
+----+----+----+--
| *0 | *1 | *2 | ...
+----+----+----+--

To get to x[1][0], for example, you need to skip sizeof x[0] bytes as in the
first example, take the pointer from there, add an index (0 in your case)
and dereference it.

I /can/ imagine a platform where sizeof x[0] is the same in both cases, i.e.
sizeof(int*) == 4*sizeof(int), but believe me, such platforms are rare.

So, int[x][y] and int(*)[0] are not equivalent types. There was a recent
post by Chris Torek on this topic, explaining exactly how the former could
decay to the latter in a function call, but that does not make it right.
(Unfortunately I don't recall the subject of that thread, sorry.)

The correct way to declare multi-dimensional arrays in function arguments
is:

int fun (int array[][X][Y][Z]);

Add as many dimensions as you like (up to six, <g>), but only the first one
can be unspecified. Your X, Y, Z etc are the same constants that you used
for declaring the array that you pass to the function.

I hope this make sense.

Peter

Peter Pichler, Feb 20, 2004