2

What I am doing wrong here for C99:

struct chess {
    struct coordinate {
        char piece;
        int alive;
    } pos[3];
}table[3] =
{
    {
      {'Q', (int)1},{'Q', (int)1},{'Q', (int)1},
      {'B', (int)1},{'B', (int)1},{'B', (int)1},
      {'K', (int)1},{'K', (int)1},{'K', (int)1},
    }
};

It gives the error:

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token

I wish to be able to access the data like having a struct within a struct that:

table[row].pos[column].piece
table[row].pos[column].alive

I tried several combinations, and none seems to work for this case. I have done previous struct hard coded initialization before that works, but not a struct within a struct as this time.

Any suggestions?

Community
  • 1
  • 1
Jessica Cohen
  • 438
  • 3
  • 12

2 Answers2

5

Try to enclose char literals in single quotes as stated above and add extra braces to make inner arrays to be initializer lists.

struct chess
{
   struct coordinate
   {
       char piece;
       int alive;
   } pos[3];
}
table[3] =
{  // table of 3 struct chess instances...
   { // ... start of a struct chess with a single member of coordinate[3]...
      { // ... this is where the  coordinate[3] array starts... 
         // ... and those are the individual elements of the  coordinate[3] array
         {'Q', 1}, {'Q', 1}, {'Q', 1}
       }
    },
    {{{'B', 1}, {'B', 1}, {'B', 1}}},
    {{{'K', 1}, {'K', 1}, {'K', 1}}}
};
Adrian Colomitchi
  • 3,974
  • 1
  • 14
  • 23
lost_guadelenn
  • 447
  • 4
  • 14
  • Can you please advice why is there double `{{}}` around raw? – Peter K. Oct 31 '16 at 08:53
  • Well, I think this is how it works: table[3] = { // init list of table[] { // init list of struct chess { // init list of pos[] {'Q', 1}, // init list of coordinate } }, {{{'B', 1}, {'B', 1}, {'B', 1}}}, {{{'K', 1}, {'K', 1}, {'K', 1}}} }; Check the following url for more examples: http://en.cppreference.com/w/c/language/struct_initialization – lost_guadelenn Oct 31 '16 at 09:07
4
struct chess {
    struct coordinate {
        char piece;
        int alive;
    } pos[3];
} table[3] =
{
    {
        .pos = {{ .piece = 'Q', .alive = 1 },
                { .piece = 'Q', .alive = 1 },
                { .piece = 'Q', .alive = 1 }
               }
    },
    {
        .pos = {{ .piece = 'B', .alive = 1 },
                { .piece = 'B', .alive = 1 },
                { .piece = 'B', .alive = 1 }
               }
    },
    {
        .pos = {{ .piece = 'K', .alive = 1 },
                { .piece = 'K', .alive = 1 },
                { .piece = 'K', .alive = 1 }
               }
    }
};

It seems to work. Just be careful about the placement of your braces, and PLEASE try to understand what you are typing. This is how to read the answer :

  1. table[3] is an array. So the initializer begin by '{' and ends by '};', with each element separated by a ','
  2. each element of this array is a 'struct chess'. So each sub-element begins by '{' and ends by '}'
  3. in each 'struct chess' you have an array 'pos[3]'. So another sub-sub '{' and '}'
  4. each 'pos' is a struct, so there is sub-sub-sub '{' and '}'
  5. finally, the fields are here. Use the C99 initializer list to make your code cleaner. And double quotes in C are char*, not char. Use single-quotes

Advices :

  • try to minimize the amount of "difficult-to-read" code. It will serve you
  • I never use nested structs. When needed, separate their codes and create function which will initialize them.
  • CPPReference is useful
  • use good variables name and identifiers for your struct. When i read 'pos', I think it is a position on a chess ((x, y) coordinates with informations).