Using assigned initializers to initialize a 2D char array initializer in a structure emits error C2078 in VS2013

I am using VS2013. The whole program is C, not C ++.

I can initialize a "string array" like this without a problem:

char titles[4][80] = { "Dad", "Idiot", "Donut Lover", "Fewl" }; // OK! 

I have a structure declared as follows:

 typedef struct { char name[80]; char titles[4][80]; } Dude; 

When I try to initialize the structure as follows:

 Dude homer = { .name = "Homer", .titles = { "Dad", "Idiot", "Donut Lover", "Fewl" } // error? }; 

I get "error C2078: too many initializers." This is due to array initialization. If I delete the line .titles = { ... , the error will disappear. Why am I getting this error? Is there any other way to initialize this type in the structure initializer?

If I changed the structure declaration to look like this:

 typedef struct { char name[80]; char *titles[4]; } Dude; 

the error disappears. This, however, is not a change I can make. Other parts of the code base require that this structure be exactly 400 bytes in size.

In addition, I fully understand that I could use strcpy to populate each field, but this does not answer my question.

+5
source share
1 answer

In C, this is easier to do:

 Dude homer = { "Homer", { "Dad", "Idiot", "Donut Lover", "Fewl" } // error? }; 

I don't know if this works, but you can try:

 Dude homer = { .name = "Homer", .titles[] = { "Dad", "Idiot", "Donut Lover", "Fewl" } // error? }; 
0
source

Source: https://habr.com/ru/post/1243906/


All Articles