C字符串 - 相同,不匹配?

Sam*_*her -2 c string posix compare

C得到一个小问题.限制自己使用简单的C(即操作系统指令),两个字符串似乎不一样.这是我的代码:

    char inputData[256];
    int rid;
    rid = read(0,inputData,256);
    // Strip input
    char command[rid];
    int i;
    for (i = 0; i<=rid-2; i++) {
        command[i] = inputData[i];
    }
    command[rid-1] = '\0';
    if (command == "exit") {
        write(1,"exit",sizeof("exit"));
    }
Run Code Online (Sandbox Code Playgroud)

现在,如果用户在查询时进入"退出"并且点击进入,则检测到"退出"的if永远不会运行.有任何想法吗?

谢谢,

编辑:我在去的时候提交git,所以当前的版本可以在github.com/samheather/octo-os找到.这显然不是完整的代码,但它证明了这个问题.

Phe*_*ide 6

您无法将字符串与==进行比较.你需要使用strcmp.

if (strcmp(command, "exit") == 0) {
Run Code Online (Sandbox Code Playgroud)

C字符串实际上是字符数组.您可以将"command"视为指向第一个字符的指针.您想要比较字符串中的每个字符,而不仅仅是第一个字符的位置.