I'm trying to initialize an array which will be a menu for my program. I want it to save the name of the option and a pointer to a function that will be called when the user selects the corresponding option.
I'm only new to C and I'm experiencing a few issues with my current setup which looks like this -
void init_menu(struct menu_item *menu)
{
/* Array of function pointers */
#if 0
BOOLEAN (*funcs)[NUMOPTIONS](struct ppd_system)
#endif
function funcs[NUMOPTIONS] =
{
display_items, purchase_item, save_system,
add_item, remove_item, reset_stock, reset_coins,
display_coins,
};
/* Array of Strings */
const char *name[NUMOPTIONS] =
{
"Display Items",
"Purchase Items",
"Save and Exit",
"Add Item",
"Remove Item",
"Display Coins",
"Reset Stock",
"Reset Coins",
"Abort Program"
};
}
My understanding is that my first array should consist of pointers to those functions, and my second array should have the name that corresponds with the function.
The problem is all the functions are returning "undeclared identifier" as they're declared in a different file (options.c, while this one is menu.c). Is there some way to pass all the options through so that they can be used in this file? (I can't change parameters as this is a homework task)
Thanks for your time.
This might be helpfull as well:
struct menu_item
{
/* the text to be displayed in the menu */
char name[MENU_NAME_LEN + 1];
/* pointer to the function to be called when this item is selected */
BOOLEAN (*function)(struct ppd_system*);
};
typedef BOOLEAN (*function)(struct ppd_system*);