Two-dimensional array to array of arrays

Discussion in 'C Programming' started by Kenneth Brody, Dec 21, 2006.

  1. Given the following current definition:

    int array[100][2];

    Is the following equivalent?

    (By "equivalent", I mean that any reference to "array[x][y]" retains
    the same meaning, and that the memory layout of the 200 ints is
    identical.)

    typedef int TwoInts[2];
    TwoInts array[100];

    What about:

    TwoInts *array = malloc( 100 * sizeof(*array) );

    Yes, I realize that type actual type of "array" has changed, and that
    malloc() may fail, and so on.

    I just need to verify that "array[x][y]" hasn't changed meaning. A
    quick test program seems to confirm that the behavior has not changed,
    but I know that implementation-defined behavior may "work" on one
    platform an not another. I'm pretty sure that the behavior here is
    defined by the standard, and not implementation-defined, but I'd
    like confirmation.

    Thanks.

    ==========
    #include <stdio.h>

    typedef int TwoInts[2];
    int array[10][2];

    int main(int argc,char *argv[])
    {
    int i,j;
    TwoInts *x;

    for ( i=0 ; i < 10 ; i++ )
    for ( j=0 ; j < 2 ; j++ )
    array[j] = i*10 + j;

    for ( i=0 ; i < 10 ; i++ )
    {
    for ( j=0 ; j < 2 ; j++ )
    printf("[%d][%d] = %2d ",i,j,array[j]);
    printf("\n");
    }

    printf("\n");
    x = array;

    for ( i=0 ; i < 10 ; i++, x++ )
    {
    for ( j=0 ; j < 2 ; j++ )
    printf("[%d][%d] = %2d ",i,j,(*x)[j]);
    printf("\n");
    }

    return(0);
    }
    ==========

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Dec 21, 2006
    #1
    1. Advertising

  2. Kenneth Brody

    Guest

    Kenneth Brody wrote:
    > Given the following current definition:
    >
    > int array[100][2];


    array is an array of 100 items, each of which is an array of 2 ints.

    > Is the following equivalent?


    > typedef int TwoInts[2];
    > TwoInts array[100];


    array is an array of 100 items, each of which is an array of 2 ints.

    >
    > What about:
    >
    > TwoInts *array = malloc( 100 * sizeof(*array) );


    array is a pointer to 100 items, each of which is an array of 2 ints.
    It can be handled as an array.

    That's my interpretation.
     
    , Dec 21, 2006
    #2
    1. Advertising

  3. Kenneth Brody

    Simon Biber Guest

    Kenneth Brody wrote:
    > Given the following current definition:
    >
    > int array[100][2];


    array has type int[100][2], which decays to an int(*)[2] pointing at the
    first two-int element of an array of 100 such elements.

    > Is the following equivalent?
    >
    > (By "equivalent", I mean that any reference to "array[x][y]" retains
    > the same meaning, and that the memory layout of the 200 ints is
    > identical.)
    >
    > typedef int TwoInts[2];
    > TwoInts array[100];


    array has the same type int[100][2], which of course decays in the same way.

    > What about:
    >
    > TwoInts *array = malloc( 100 * sizeof(*array) );


    array is an int(*)[2] and, assuming malloc succeeded, points at the
    first two-int element of an array of 100 such elements.

    They decay to the same thing, when used in any other way apart from as
    the operand of unary & or sizeof.

    It may be used identically to the first two, except as an operand to
    unary & or sizeof.

    In the first two cases case &array is int(*)[100][2]
    In the last case &array is int(**)[2]

    In the first two cases sizeof array is 100*2*sizeof(int)
    In the last case, sizeof array is sizeof(int(*)[2]) which is usually the
    same size as any other object pointer: 2, 4 or 8 bytes are typical.

    --
    Simon.
     
    Simon Biber, Dec 21, 2006
    #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. Alf P. Steinbach
    Replies:
    0
    Views:
    455
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,958
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    689
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    1,019
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    230
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page