【发布时间】:2018-12-27 20:17:06
【问题描述】:
无论哪种情况,Switch 语句都会导致“Segmentation fault (core dumped)”。
我尝试更改“命令”的数据类型,但无法得到任何其他结果。
char command;
int temp;
while(1) {
printf("Enter command ('d'/'m'/'s'/'r'): ");
scanf("%c", command);
printf("\n");
switch(command) {
case 'd' :
printf("display which employee (0-19)?\n");
scanf("%i", temp);
//display(temp);
printf("displayed");
break;
case 'm' :
printf("modify which employee (0-19)?\n");
scanf("%i", temp);
//modify(temp);
printf("modified");
break;
case 's' :
//save();
printf("saved");
break;
case 'r' :
//retrieve();
printf("retrieved");
break;
default :
printf("Command not recognized\n");
}
}
预计根据相关案例打印动作。相反,它只是打印“分段错误(核心转储)”消息。
【问题讨论】:
-
scanf("%i", temp);-->scanf("%i", &temp);由于temp是一个int变量,您需要提供&以将用户输入存储到其中。还要仔细阅读编译器警告并解决它们,不要忽略它们。
标签: c