3

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?

4
  • 4
    piece is a char, "Q" is a string. Use simple quotes. Commented Oct 31, 2016 at 8:37
  • 3
    Please read your error messages; I'm pretty sure your compiler would have told you it cannot convert a char * to a char, which would tell you exactly what your problem is. Commented Oct 31, 2016 at 8:39
  • 4
    And there is no need to cast 1 to int as it is already of that type. Commented Oct 31, 2016 at 8:41
  • 2
    @Jean-FrançoisFabre: You mean "single"? Commented Oct 31, 2016 at 8:41

2 Answers 2

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}}}
};
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please advice why is there double {{}} around raw?
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: en.cppreference.com/w/c/language/struct_initialization
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.