multi dimension arrays

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

  1. Robert

    Robert Guest

    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
    #1
    1. Advertising

  2. "Robert" <> wrote in message
    news:p...
    > 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
    #2
    1. Advertising

  3. "Robert" <> wrote in message
    news:p...
    > 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
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,680
    Jim Lewis
    Mar 21, 2006
  2. Adam Hartshorne

    Multi-Dimension Array Question

    Adam Hartshorne, Jun 8, 2005, in forum: C++
    Replies:
    6
    Views:
    2,147
  3. DiAvOl
    Replies:
    17
    Views:
    578
    John Bode
    Jan 7, 2008
  4. Replies:
    123
    Views:
    2,048
    Tim Rentsch
    Oct 9, 2008
  5. Luuk
    Replies:
    15
    Views:
    835
    Nobody
    Feb 11, 2010
Loading...

Share This Page