我的代码假设循环执行命令,然后执行命令直到命令退出。当它运行时,我得到一个永无止境的循环。
void run(){
char command[100][100], *p;
int numOfArgs;
while(1){
p=&command[0][0];
numOfArgs = 0;
while(getchar()!= '\n'){
while(getchar()!= ' '){
*p=getchar();
p++; //increased to next char in string
}
*p='\0';
numOfArgs++; //increases number of strings
p=&command[numOfArgs][0]; //References p to location 0 of next string
}
if(strcmp(command[0], "/*command*/") == 0){
//Do command
}
if(strcmp(command[0], "exit") == 0)
return;
else printf("Not a valid command");
}
}
最佳答案
将第 13 行更改为
p=&command[numOfArgs][0];
您还需要使用 \0
终止您的命令。
关于c - 循环命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19232669/