This is more than you ask, but should be useful:
void M1P1() { puts("Hey! You selected menu 1 position 1!"); }
void M1P2() { puts("Hey! You selected menu 1 position 2!"); }
void M1P3() { puts("Hey! You selected menu 1 position 3!"); }
void M2P1() { puts("Hey! You selected menu 2 position 1!"); }
void M2P2() { puts("Hey! You selected menu 2 position 2!"); }
typedef struct {
char *caption;
void (*action)();
} SubMenuItem;
SubMenuItem sub_menu1[] = {
{ "m1p1", M1P1 },
{ "m1p2", M1P2 },
{ "m1p3", M1P3 },
};
SubMenuItem sub_menu2[] = {
{ "m2p1", M2P1 },
{ "m2p2", M2P2 },
};
typedef struct {
char *caption;
SubMenuItem *sub_menus;
unsigned sub_menus_count;
} MenuItem;
MenuItem menu[] = {
{ "menu1", sub_menu1, sizeof(sub_menu1) / sizeof(sub_menu1[0]) },
{ "menu2", sub_menu2, sizeof(sub_menu2) / sizeof(sub_menu2[0]) },
};
#define MENU_ITEMS_COUNT (sizeof(menu) / sizeof(menu[0]));
int i, j;
for (i = 0; i < MENU_ITEMS_COUNT; i++) {
printf("%d) %s\n", i + 1, menu[i].caption);
for (j = 0; j < menu[i].sub_menus_count; j++) {
printf("\t%d.%d) %s\n", i + 1, j + 1, menu[i].sub_menus[j].caption);
}
}
putchar('\n');
menu[0].sub_menus[1].action();
adf88 source
share