我创建了一个char *,它本质上将被视为一个字符串。该字符串被假设为一遍又一遍地被使用。每次我试图检查while循环,看看它是否正确的“退出”...使用scanf得到一个字符串来创建并验证它反对while循环...不断得到
*我一直得到分段错误...我在做什么错 - 漂亮的白痴错误 - 可能?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
char* input = (char*)malloc(sizeof(char));
input = "CONTINUE";
while(strcmp(input, "quit") != 0)
{
printf("%s", "System: ");
scanf("%s", input);
}
return 0;
}
所以如果我做了... char * input =(char *)malloc(LENGTH); //长度大概是80或100 ... 然后我可以继续做while循环,总是会重写字符串? –
是的。只要不指定“CONTINUE”或像我向你展示的那样使用'strcpy'。 – cnicutar
非常好 - 谢谢很多人。就像我说的,非常愚蠢的错误。 –