I’m creating a lookup table in C
When I define this:
typedef struct {
char* action;
char* message;
} lookuptab;
lookuptab tab[] = {
{"aa","bb"},
{"cc","dd"}
};
it compiles without errors but when I do something like this:
typedef struct {
char* action;
char* message[];
} lookuptab;
lookuptab tab[] = {
{"aaa", {"bbbb", "ccc"}},
{"cc", {"dd", "eeeee"}}
};
I get the following error:
error: initialization of flexible
array member in a nested contexterror: (near initialization for
‘tab[0].message’)
How can I initialize the tab array in the second example?
Note: I know all the values inside the tab array.
UPDATE: message could be of different size, e.g
typedef struct {
char* action;
char* message[];
} lookuptab;
lookuptab tab[] = {
{"aaa", {"bbbb", "ccc", "dd"}},
{"cc", {"dd", "eeeee"}}
};
Thank you very much.
Best regards,
Victor
You can’t use structures containing a flexible array member in an array (of the structure). See C99 standard §6.7.2.1/2:
So, use a
char **instead (and worry about how you know how many entries there are):This uses a C99 construct (§6.5.2.5 Compound literals) – beware if you are not using a C99 compiler.