使用未初始化的指针
start
,do/while循环更适合在使用
strcmp
.
我不确定
%ms
为每个调用分配一个新缓冲区。由于缓冲区不需要初始化,我怀疑它分配了一个新的缓冲区。为了避免内存泄漏,
free
缓冲区在需要之前和不再需要之后。
后面的空间
%质谱
将使用所有尾随空白。要终止扫描,必须输入一些非空格。把后面的空格移到下一个
scanf
在第一次之前
%c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char *start = NULL;
char *end = NULL;
char in, out, shift;
do {
if ( start) {
free ( start);
start = NULL;
}
printf("before start: ");
fflush ( stdout);
scanf("%ms", &start);
if(strcmp(start, "acc") != 0) {
if ( end) {
free ( end);
end = NULL;
}
printf("in if: ");
fflush ( stdout);
scanf(" %c %c %c %ms", &in, &out, &shift, &end);
printf("%s", start);
printf("%c", in);
printf("%c", out);
printf("%c", shift);
printf("%s", end);
}
} while ( strcmp(start, "acc") != 0);
if ( start) {
free ( start);
start = NULL;
}
if ( end) {
free ( end);
end = NULL;
}
return 0;
}