关于gets()函数报Stack错误问题
问题描述:
大概如下代码
char tmp;
int x=0,y=0;
gets(&tmp);
x = tmp -48;
gets(&tmp);
y = tmp -48;
运行输入两次后提示:Stack around the variable 'tmp' was corrupted。
修改代码如下后不报错:
char *tmp = (char*)malloc(sizeof(char));
int x=0,y=0;
gets(tmp);
x = (*tmp) -48;
gets(tmp);
y = (*tmp)-48;
请教各位大神,这是什么原因呢?
[解决办法]
问题描述:
大概如下代码
char tmp; 此处不是数组 此处如果想定义数组 则没有分配足够的空间
int x=0,y=0;
gets(&tmp); 所以要用getchar(tmp) gets(tmp) 无法存放'\0'
x = tmp -48;
gets(&tmp);
y = tmp -48