C multi-dimensional arrays and pointers

Discussion in 'C Programming' started by Richard Hayden, Nov 1, 2004.

  1. Hi,

    Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
    type' warning when compiling:

    int main(int argc, char** argv) {
    int testa[2][2];
    int** testp = testa;
    }

    Whereas the following code works without warnings:

    int main(int argc, char** argv) {
    int testa[2];
    int* testp = testa;
    }

    I thought that multi-dimensional arrays were implemented as arrays of
    pointers to arrays of pointers?

    Any insight gratefully appreciated!

    Thanks,

    Richard Hayden.
     
    Richard Hayden, Nov 1, 2004
    #1
    1. Advertising

  2. In article <cm6dud$at5$>,
    Richard Hayden <> wrote:
    >Hi,
    >
    >Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
    >type' warning when compiling:
    >
    >int main(int argc, char** argv) {
    > int testa[2][2];
    > int** testp = testa;
    >}


    testa is an array[2] of array[2] of int:
    [ [ [int1] [int2] ] [ [int1] [int2] ] ]

    In most contexts (including the assignment), the array name decays to
    a pointer to array[2] of int:
    +-testa-decayed-to-pointer
    v
    [ [ [int1] [int2] ] [ [int1] [int2] ] ]


    >Whereas the following code works without warnings:
    >
    >int main(int argc, char** argv) {
    > int testa[2];
    > int* testp = testa;
    >}


    testa is an array[2] of int:
    [ [int1] [int2] ]

    In most contexts (including teh assignment), the array name decays to
    a pointer to int:
    +-testa-decayed-to-pointer
    v
    [ [int1] [int2] ]


    >I thought that multi-dimensional arrays were implemented as arrays of
    >pointers to arrays of pointers?


    Multi-dimensional arrays are implemented as arrays of arrays. Pointers
    have nothing to do with it. (Don't confuse the fact that the name of an
    array decays to a pointer to its first element with the implementation
    thereof.)


    >Any insight gratefully appreciated!


    Googling for Chris Torek's posts on the subject will yield further
    insight.


    dave

    --
    Dave Vandervies
    >What problem am I missing?

    Your brain is switched on, and mine wasn't.
    --Gergo Barany and Chris Dollin in comp.lang.c
     
    Dave Vandervies, Nov 1, 2004
    #2
    1. Advertising

  3. Richard Hayden

    Eric Sosman Guest

    Richard Hayden wrote:
    > Hi,
    >
    > Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
    > type' warning when compiling:
    >
    > int main(int argc, char** argv) {
    > int testa[2][2];
    > int** testp = testa;
    > }
    >
    > Whereas the following code works without warnings:
    >
    > int main(int argc, char** argv) {
    > int testa[2];
    > int* testp = testa;
    > }
    >
    > I thought that multi-dimensional arrays were implemented as arrays of
    > pointers to arrays of pointers?
    >
    > Any insight gratefully appreciated!


    Please see Section 6 of the comp.lang.c Frequently
    Asked Questions (FAQ) list

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

    .... and then come back if you're still confused.

    --
     
    Eric Sosman, Nov 1, 2004
    #3
  4. Richard Hayden wrote:
    > Hi,
    >
    > Why does gcc (3.3.2) give me a 'initialization from incompatible pointer
    > type' warning when compiling:
    >
    > int main(int argc, char** argv) {
    > int testa[2][2];
    > int** testp = testa;

    int (*testp)[2] = testa;
    would be OK.
    > }
    >
    > Whereas the following code works without warnings:
    >
    > int main(int argc, char** argv) {
    > int testa[2];
    > int* testp = testa;
    > }
    >
    > I thought that multi-dimensional arrays were implemented as arrays of
    > pointers to arrays of pointers?

    No (unlike Pascal). They are implemented as "flat" arrays: se section
    6.5.2.1 of the standard.

    Robert

    >
    > Any insight gratefully appreciated!
    >
    > Thanks,
    >
    > Richard Hayden.
     
    Robert Harris, Nov 1, 2004
    #4
  5. On Mon, 01 Nov 2004 23:02:21 GMT, Robert Harris
    <> wrote:

    > Richard Hayden wrote:

    <snip>
    > > I thought that multi-dimensional arrays were implemented as arrays of
    > > pointers to arrays of pointers?

    > No (unlike Pascal). They are implemented as "flat" arrays: se section
    > 6.5.2.1 of the standard.
    >

    Correct about C, or nearly so; they are actually array of array, which
    works out (effectively) to row-major consecutive memory. But this is
    not different from Pascal, which like most HLLs has multidim arrays in
    the obvious fashion -- although it tries harder than C or at least
    typical C implementations to prevent out-of-bounds subscripting (or
    equivalently in C only pointer arithmetic) which is the only way a
    program can actually see this.

    Aside from C's predecessors B and BCPL, now almost entirely vanished,
    the only language I know of that requires array of pointer to array
    etc. is Java, although the pointers (and elements) are "Object"s that
    are implicitly dereferenced. C++ has C-equivalent arrays including
    multidim, but also std::vector which if nested becomes array of
    (hidden) pointer to array etc.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Nov 8, 2004
    #5
    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:
    436
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,928
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    657
    Vivek
    Aug 21, 2003
  4. Replies:
    4
    Views:
    293
    Tosha
    Aug 2, 2006
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    203
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page