I'm developing a simple but flexible menu system for an embedded system in C. Due to limitations of the platform , I want to avoid dynamic allocation and want everything to be defined statically.
I have an Menu type and a MenuItem type defined as structs, with a Menu containing multiple MenuItems. Each menu item will have a Label, ItemOptionType and then a pointer to an array of Options.
That part that's not working for me is creating a reference to the Option array. I get a warning saying
warning: incompatible pointer types initializing 'char *' with an expression of type 'char *(*)[3]' [-Wincompatible-pointer-types]
MenuItem menu_item1 = {"Day", OPTION_LIST, 0,0,0, 4, &dayOptionList};
Any suggestions on the right approach would be much appreciated.
#define MAX_MENU_ITEMS 16
typedef enum {OPTION_LIST, OPTION_NUMERIC, OPTION_BINARY} ItemOptionType;
typedef struct MenuItem {
char* label;
ItemOptionType optionType;
unsigned char min;
unsigned char max;
unsigned char value;
unsigned char optionCount;
char* optionList[];
} MenuItem;
typedef struct Menu {
unsigned char count;
MenuItem* items[MAX_MENU_ITEMS];
} Menu;
unsigned int MenuAddItem(Menu* menu, MenuItem* item) {
if (menu->count < MAX_MENU_ITEMS) {
menu->items[menu->count] = item;
menu->count++;
return 1;
}
return 0;
}
char* dayOptionList[] = {"Monday", "Tuesday", "Wednesday" "Thursday", "Friday", "Saturday", "Sunday"};
Menu mainMenu;
MenuItem menu_item1 = {"Day", OPTION_LIST, 0,0,0, 4, &dayOptionList};
MenuItem menu_item2 = {"Age", OPTION_NUMERIC, 0, 120, 25, 0, NULL};
int main(int argc, char *argv[]) {
MenuAddItem(&mainMenu, &menu_item1);
MenuAddItem(&mainMenu, &menu_item2);
while(1);
}
char* optionList[];is a flexible array member. You can't have those without dynamic allocation. What you probably want is something likechar **. And then initializeoptionListusing plaindayOptionList(without the pointer-toi operator*).menu_item1. Please copy-paste the full and complete build output from the code you actually show.