简体   繁体   English

C while循环条件

[英]C while loop condition

I'm reading Computer Systems for my CS class and I've come across a while loop condition that's puzzling me, here's the code: 我正在为我的CS课阅读计算机系统,我遇到了令我困惑的while循环条件,这里是代码:

int parseline(char *buf, char **argv)
{
    char *delim; /* Points to first space delimiter */
    int argc; /* Number of args */
    int bg; /* Background job? */

    buf[strlen(buf)-1] = ’ ’; /* Replace trailing ’\n’ with space */
    while (*buf && (*buf == ’ ’)) /* Ignore leading spaces */
        buf++;

     /* Build the argv list */
     argc = 0;
     while ((delim = strchr(buf, ’ ’))) {
         argv[argc++] = buf;
         *delim = ’\0’;
          buf = delim + 1;
          while (*buf && (*buf == ’ ’)) /* Ignore spaces */
              buf++;
     }

In

while (*buf && (*buf == ’ ’)) /* Ignore spaces */ 

the while loop has two operands to logical && but I don't understand what is the purpose of the first operand (*buf) . while循环有两个逻辑&&操作数,但我不明白第一个操作数(*buf)的用途是什么。 The second operand is checking for empty space, but I would think that the second operand by itself would suffice for the purpose of this loop. 第二个操作数是检查空白空间,但我认为第二个操作数本身就足以满足此循环的目的。

Yes, the *buf && is superfluous. 是的, *buf &&是多余的。


*buf is false for '\\0' and true for everything else. *buf对于'\\0'*buf为false,对于其他所有内容为true。

*buf == ' ' is true for ' ' and false for everything else, including '\\0' . *buf == ' '是真正的' '假的一切, 包括 '\\0'

The following is functionally the same as while (buf == ' ') if the quote marks ' are changed to ' . 以下是在功能上相同, while (buf == ' ')如果引号'改变为'

//                      v-v--- not standard quote marks.
while (*buf && (*buf == ’ ’))  

With a good compiler, neither is faster as an optimizing compiler with emit the same code. 使用良好的编译器,作为具有相同代码的优化编译器,两者都不会更快。

To me it is simply pedantic code insuring the loop is not taken with a null character . 对我来说,这只是一种迂腐的代码,确保循环不会带有空字符


What is bad about the code includes: 代码有什么不好之处包括:

buf[strlen(buf)-1] = ' '; is a UB if buf[0] == 0 . 如果buf[0] == 0则为UB。

buf[strlen(buf)-1] = ' '; /* Replace trailing '\\n' with space */ buf[strlen(buf)-1] = ' '; /* Replace trailing '\\n' with space */ may lop off a non- '\\n' . buf[strlen(buf)-1] = ' '; /* Replace trailing '\\n' with space */可能会删掉非'\\n'

A better alternative that address the 2 preceding concerns: buf[strcspn(buf, "\\n")] = '\\0'; 解决前两个问题的更好的替代方案: buf[strcspn(buf, "\\n")] = '\\0';

Instead of "Ignoring spaces", it is more C-like to ignore white-spaces . 相反,“忽略空格”的,它更类似于C忽略的空格

"Build the argv list" usually requires a final argv[argc] == NULL . “构建argv列表”通常需要最终的argv[argc] == NULL

Of course these are sides issues to the main question and without the larger context may/may not apply. 当然,这些是主要问题的侧面问题,没有更大的背景可能/可能不适用。

The second operand is checking for empty space, but I would think that the second operand by itself would suffice for the purpose of this loop. 第二个操作数是检查空白空间,但我认为第二个操作数本身就足以满足此循环的目的。

  while (*buf && (*buf == ’ ’)) /* Ignore leading spaces */
        buf++;

It would suffice. 这就足够了。 The loop would break for *buf == '\\0' . 循环会因*buf == '\\0'而中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM