An array of structures in C...

Posted by 00010000 on Stack Overflow See other posts from Stack Overflow or by 00010000
Published on 2010-04-26T21:06:58Z Indexed on 2010/04/26 21:43 UTC
Read the original article Hit count: 146

Filed under:
|
|

For the life of me I can't figure out the proper syntax for creating an array of structures in C. I tried this:

struct foo {
    int x;
    int y;
} foo[][] = {

    {
        { 1, 2 },
        { 4, 5 },
        { -1, -1 }
    },

    {
        { 55, 44 }
        { 100, 200 },
    }
};

So for example foo[1][0].x == 100, foo[0][1].y == 5, etc. But GCC spits out a lot of errors.

If anyone could provide the proper syntax that'd be great.

EDIT: Okay, I tried this:

struct foo {
     const char *x;
     int y;
};

struct foo bar[2][] = {

     {
      { "A", 1 },
      { "B", 2 },
      { NULL, -1 },
     },

     {
      { "AA", 11 },
      { "BB", 22 },
      { NULL, -1 },
     },

     {
      { "ZZ", 11 },
      { "YY", 22 },
      { NULL, -1 },
     },

     {
      { "XX", 11 },
      { "UU", 22 },
      { NULL, -1 },
     },
};

But GCC gives me "elements of array bar have incomplete type" and "excess elements in array initializer".

© Stack Overflow or respective owner

Related posts about c

    Related posts about array