I have written the following basic code for a menu:
typedef struct Menu {
char* title;
unsigned num_submenus;
struct Menu *submenu[];
} Menu;
Menu sub1 = {"Submenu 1", 0, {NULL}};
Menu sub2 = {"Submenu 2", 0, {NULL}};
Menu Main = {"Main Menu", 2, {&sub1, &sub2}}; /* No Error?! */
int main()
{
printf("%s\n", Main.title);
printf("%s\n", Main.submenu[0]->title);
printf("%s\n", Main.submenu[1]->title);
}
Browsing through a few related questions it seems like the only way to use a flexible array member is to dynamically allocate memory to it. However my compiler is perfectly happy to compile and run the code without any errors or warnings. Is this verboten?
I am using MinGW gcc 4.6.1 and compiling under C99 rules.
-pedantic
that will give an error. – Constipationclang++ 11.0.0
gives:error: initialization of flexible array member is not allowed
. – Reproduction