Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > multi dimension arrays

Reply
Thread Tools

multi dimension arrays

 
 
Robert
Guest
Posts: n/a
 
      02-18-2004
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


 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      02-18-2004
"Robert" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Remove the 'g' from my address to send me mail.


 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      02-20-2004
"Robert" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a question about 1-dimension and 2-dimension array Luuk C Programming 15 02-11-2010 02:45 AM
multi dimensional arrays as one dimension array vippstar@gmail.com C Programming 123 10-09-2008 06:46 AM
Question about pointers and multi-dimension arrays DiAvOl C Programming 17 01-07-2008 06:30 PM
multiD-vhdl: Multi Dimensional Arrays (allowing generics on each dimension) for VHDL (including ports) albert.neu@gmail.com VHDL 2 03-21-2006 04:05 PM
newbie question : picture dimension vs print dimension Rene Wong Digital Photography 9 09-30-2003 01:46 AM



Advertisments