【发布时间】:2015-06-22 14:05:59
【问题描述】:
使用*s 而不是char s 时出现分段错误。
如果我将*s 更改为s,我会收到名为char 整数指针的错误。请帮我找出错误。我也用Google 搜索过,但无法更正。
#include<stdio.h>
main()
{
char *s,o,a,b,j[20];
printf("code generation\n----------------------");
printf("Enter the code:\n");
scanf("%s",j);
int c=1;
while(c==1){
o=j[1];
a=j[0];
b=j[2];
switch(o){
case '+':
s="ADD";
break;
case '-':
s="SUB";
break;
case '*':
s="MUL";
break;
case '/':
s="DIV";
break;
case '=':
s="MOV";
break;
default:
s="INV";
}
printf("Code Generated:%s %s , %s\n",s,a,b);
}
}
【问题讨论】:
-
@AlterMann 我只是编辑并修复了它
-
如果 1) 用户输入的内容超过 19 个字符 2) 如果用户只输入一个字符并且 3) 请参阅 @SouravGhosh 的回答,则您的代码会出现未定义行为
-
C 具有允许名称中包含多个字符的功能,主要是为了使它们自动记录。下次您提出问题时,请使用更具描述性的名称。
标签: c segmentation-fault printf format-specifiers