与“>”比较时,while 循环条件永远不会计算为 true。

发布于 2024-11-05 12:58:36 字数 384 浏览 0 评论 0原文

我正在使用 while 循环来检测“>”的存在数组中的符号。循环条件永远不会计算为 true。当我显示数组的内容时,“>”不过,符号本身就存在。在我的代码中,这不是第一次调用 strtok。这也只是总代码的一小部分。要被 strtok 分解的字符串是“ls > testfile.txt”。任何帮助将不胜感激。

while (userArgs[k] != ">") {

    k++;

    userArgs[k] = strtok(NULL, " ");
    if(userArgs[k] == ">") {
        break;

    }
    if (userArgs[k] == NULL){
        break;
    }
}

I am using a while loop to detect the presence of a ">" symbol in an array. The loop conditional never evaluates to true. When I show the contents of the array, the ">" symbol is there by itself though. In my code, this isn't the first call to strtok. This is also only a snippet of the total code. The string to be broken up by strtok is "ls > testfile.txt". Any help would be appreciated.

while (userArgs[k] != ">") {

    k++;

    userArgs[k] = strtok(NULL, " ");
    if(userArgs[k] == ">") {
        break;

    }
    if (userArgs[k] == NULL){
        break;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘落散花 2024-11-12 12:58:36

strtok 返回一个 char * 所以我猜你正在尝试比较字符串。

您无法像 C 中那样比较字符串,请使用 strcmp。你现在所做的是比较地址,绝对不是你想要的。

strtok returns a char * so I guess you're trying to compare strings.

You can't compare strings like that in C, use strcmp. What you are doing now is comparing addresses, definitely not what you want.

指尖上得阳光 2024-11-12 12:58:36

IF userArgs 是 char 类型,那么您正在将字符串与字符常量进行比较 while (userArgs[k] != ">")if(userArgs [k] == ">") 所以实际上是将二进制程序文件中字符串的地址与字符值进行比较。请注意,">" 是一个字符串常量,由存储该字符串的某个地址表示。'>' 是一个字符常量,并且有一个值。

IF userArgschar * 那么您正在比较程序堆栈或堆中的 userArgs[i] 位置的地址(如果是动态分配的)存储在程序二进制文件中并由操作系统加载到内存中的 ">" 字符串的地址。这些内存位置会随着时间的推移而变化,并且与其内容无关。要比较内存的内容,您必须使用while (strcmp (userArgs[k], ">") != 0)。或者为了避免调用 strcmp 您可能需要执行以下 while ((userArgs[k][0] != '>') && (userArgs[k][1 ] == '\0')

上述示例需要根据您的需要进行更改。

IF userArgs is of type char then you are comparing a string with a character constant while (userArgs[k] != ">") and if(userArgs[k] == ">") So actually the address of the strings in the binary program file is compared to the character value. Note that the ">" is a string constant and is represented by some address where the string is stored '>' is a character constant and has a value.

IF userArgs is char * then you are comparing the address of the userArgs[i] location in the program stack or heap (if dynamically allocated) with the address of ">" string stored in the program binary and loaded by the OS on the memory. These memory location will vary from time to time and has no relation with the contents of it. To compare the contents of the memories you have to use while (strcmp (userArgs[k], ">") != 0). OR to avoid calling strcmp you might want to do the following while ((userArgs[k][0] != '>') && (userArgs[k][1] == '\0')

The above examples would require change as per your needs.

甩你一脸翔 2024-11-12 12:58:36

使用 '>' 而不是 ">"。第一个是字符,第二个是字符串 - 或者实际上是指向字符的指针,并且比较指针显然不是您想要的。

Use '>' instead of ">". The first one is a character and the second one is a string - or actually a pointer to a character, and comparing pointers clearly isn't what you want.

ゃ人海孤独症 2024-11-12 12:58:36

您正在比较地址 userArgs[k] 是否与“<”的堆栈地址相同。但事实并非如此。您需要匹配 userArgs[k][0] == '>' // 注意单引号。 但如果 '>' 则这是错误的前面没有空格。

You are comparing if the address userArgs[k] is the same as the stack address for "<". This is never the case. You would need to match userArgs[k][0] == '>' // note the single quote. but this wrong if the '>' is not preceded by a space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文