我首先在编译时警告有两个问题:警告:ISO C90禁止混合声明和代码[-Wdeclaration-after-statement] MenuFunction fp;
和第二个我总是结束“错误:缓冲区溢出。请再试一次,输入更少的数据”我做错了什么我不知道,请帮我解决这个问题。
typedef void (*MenuFunction)(System*);
int main(int argc, char ** argv)
{
...
/* While loop for my menu */
while(1)
{
printf("Main Menu\n");
printf("%s\n", menu[0].text);
printf("%s\n", menu[1].text);
printf("%s\n", menu[2].text);
printf("Select your option (1-3): ");
MenuFunction fp;
fp = getMenuChoice(menu);
if(fp == NULL){
fprintf(stderr, "invalid choice\n");
}
else{
(*fp)(&system);
}
}
}
/* Function that points to the menu function */
MenuFunction getMenuChoice(MenuItem * menu)
{
MenuFunction function = NULL;
char select[50];
fgets(select, 50, stdin);
if(select[strlen(select)-1] == '\n')
{
switch(select[0])
{
case '1':
function = menu[0].function;
break;
case '2':
function = menu[1].function;
break;
case '3':
function = menu[2].function;
exit(0);
break;
default:
printf("Invalid option\n");
}
}
else
{
readRestOfLine();
printf("Error: buffer overflow. Please try again, entering less data");
}
return function;
}