我正在尝试初始化一个数组,它将成为我程序的菜单。 我希望它保存选项的名称和指向当用户选择相应选项时将调用的函数的指针。
我只是C的新手,我遇到了一些与我当前设置有关的问题 -
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"
};
}
我的理解是我的第一个数组应该包含指向这些函数的指针,而我的第二个数组应该具有与该函数对应的名称。
问题是所有函数都返回“未声明的标识符”,因为它们在不同的文件中声明(options.c,而这个是menu.c)。有没有办法通过所有选项,以便它们可以在这个文件中使用? (我无法更改参数,因为这是一项家庭作业)
感谢您的时间。
这也可能有所帮助:
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*);