I'm very new to programming so be gentle, :(
Essentially what I would like to do is to have a single function to be able to create a dynamic array of pointers to a struct so that I can have a modular menu system up and running. My menus will have several varying numbers of options. This is the first time I've worked with something like this and just can't get it worked out.
I've snipped a lot of unnecessary code to this smaller piece so I can work out the data flow. These are the exact same kinds of errors I get in the larger program though.
As you can tell from the errors, I'm fairly lost. The errors I get are:
I'm lost in a maze of array, pointers, and void pointers, :( As far as the error on line 27, I'm not sure why 'i' is undeclared as I have it and use it in the function before that.
Thanks for any help you can give!
Essentially what I would like to do is to have a single function to be able to create a dynamic array of pointers to a struct so that I can have a modular menu system up and running. My menus will have several varying numbers of options. This is the first time I've worked with something like this and just can't get it worked out.
I've snipped a lot of unnecessary code to this smaller piece so I can work out the data flow. These are the exact same kinds of errors I get in the larger program though.
Code:
#include <stdio.h> #include <stdlib.h> struct menuStruct { char question[100]; void *menuCommand; struct menuStruct *nextMenu[]; }; //end menuStruct typedef struct menuStruct *searchNameMenu[1]; void addMenuItem(menuStruct **menu[], char question[], void menuCommand(), menuStruct **nextMenu[]) { int i = 0; menuStruct *newOption; newOption = malloc(sizeof(menuStruct)); strncpy(newOption->question, question, 100); newOption->menuCommand = menuCommand; newOption->nextMenu = &nextMenu; menu = realloc(menu, ((sizeof(menu[]) + sizeof(menuStruct*))); while(menu[i] != NULL) i++; *menu[i] = newOption; } //end function addMenuItem void initSearchNameMenu() { addMenuItem(searchNameMenu, "Search names by name.", searchNameDB_Name, NULL); addMenuItem(searchNameMenu, "Return to Previous Menu.", returnPrevMenu, NULL); addMenuItem(searchNameMenu, "Return to Main Menu.", returnMainMenu, NULL); } //end function initSearchNameDBMenu
Code:
c:12: error: syntax error before '*' token c:12: error: syntax error before '*' token c:15: warning: data definition has no type or storage class c:17: error: conflicting types for 'newOption' c:15: error: previous declaration of 'newOption' was here c:17: warning: initialization makes integer from pointer without a cast c:17: error: initializer element is not constant c:17: warning: data definition has no type or storage class c:18: error: syntax error before '->' token c:18: warning: conflicting types for built-in function 'strncpy' c:18: warning: data definition has no type or storage class c:19: error: syntax error before '->' token c:22: error: syntax error before ']' token c:22: error: syntax error before ')' token c:22: error: syntax error before ';' token c:22: warning: data definition has no type or storage class c:27: error: `i' undeclared here (not in a function) c:27: warning: data definition has no type or storage class c:28: error: syntax error before '}' token c: In function `initSearchNameMenu': c:35: error: syntax error before "searchNameMenu" c:36: error: syntax error before "searchNameMenu" c:37: error: syntax error before "searchNameMenu" c: At top level: c:12: error: storage size of `menuStruct' isn't known c:27: error: storage size of `menu' isn't known
Thanks for any help you can give!
Comment