I'm using VS2013. The whole program is C, not C++.
I can initialize an "array of strings" like this without any problems:
char titles[4][80] = { "Dad", "Idiot", "Donut Lover", "Fewl" }; // OK!
I have a struct declared like this:
typedef struct
{
char name[80];
char titles[4][80];
} Dude;
When I try to initialize the struct like this:
Dude homer =
{
.name = "Homer",
.titles = { "Dad", "Idiot", "Donut Lover", "Fewl" } // error?
};
I get an "error C2078: too many initializers". This is because of the array initialization- If I remove the .titles = { ...
line, the error goes away. Why am I getting this error? Is there a different way to accomplish this type of string initialization within a struct initializer?
If I change the declaration of the struct to look like this
typedef struct
{
char name[80];
char *titles[4];
} Dude;
the error goes away. This is, however, not a change I can make. Other parts of the code base require that the size of this struct is exactly 400 bytes.
Further, I'm quite aware that I could use strcpy
to fill in each field, but that does not answer my question.
In C, it's easier to do this:
Dude homer =
{
"Homer",
{ "Dad", "Idiot", "Donut Lover", "Fewl" } // error?
};
Don't know if this works, but you can try:
Dude homer =
{
.name = "Homer",
.titles[] = { "Dad", "Idiot", "Donut Lover", "Fewl" } // error?
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With