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
", Main.title);
printf("%s
", Main.submenu[0]->title);
printf("%s
", 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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…