c - 循环命令

标签 c

我的代码假设循环执行命令,然后执行命令直到命令退出。当它运行时,我得到一个永无止境的循环。

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/

相关文章:

c - 无论内容如何,​​写入给定字节数的字符串

c++ - FindFirstFile 不适用于通配符

c - 如何用 lseek() 判断偏移光标是否在 EOF 处?

c - 没有正确显示矩阵的转置?

c - 英特尔 SSE 内在函数 _mm_load_si128 段错误,

c - 向指针地址添加 0 填充

c - 如何将 char* 转换为 UTF-8 编码的 char*?

gdb 可以调试 gcc 风格的嵌套 C 函数吗?

c - 将数组传递给 C 中的函数

wait() 后检查状态