【问题标题】:What does while(*pointer) means in C?C 语言中的 while(*pointer) 是什么意思?
【发布时间】:2015-11-16 04:32:34
【问题描述】:

当我最近看一些关于 C 指针的文章时,我发现了一些有趣的东西。它说的是,这样的代码:

char var[10];
char *pointer = &var;
while(*pointer!='\0'){
    //Something To loop
}

可以变成这样:

//While Loop Part:
while(*pointer){
    //Something to Loop
}

那么,我的问题是,*pointer 是什么意思?

【问题讨论】:

  • *pointer“指向”字符串var的第一个字符;当第一个字符为 0(数字上,不是字符 '0')时,退出循环
  • 打开任意一本C书,仔细阅读解释while的部分。
  • .... 希望 C 书也能解释为什么你不应该这样做,因为它会导致这种混乱;)

标签: c loops pointers


【解决方案1】:

*pointer 表示取消引用存储在pointer 指向的位置的值。当pointer指向一个字符串并像while(*pointer)一样在while循环中使用时,相当于while(*pointer != '\0'): loop util null terminator if found。

【讨论】:

    【解决方案2】:

    是的,你可以去。

    请注意*pointerpointer 指向(或保存地址)的内存位置的值。

    您的*pointer 现在指向字符数组var 的各个字符。

    所以,while(*pointer) 是等价物的速记用法

    while(*pointer!='\0').
    

    假设,您的字符串被初始化为 9 个字符,例如“123456789”,并位于地址为 addr(内存位置)。

    现在因为声明:

    char *pointer=&var;
    

    pointer 将指向字符串“1234567890”的第一个元素。

    当您编写*pointer 时,它将检索存储在内存位置addr 的值,即1

    现在,声明:

    while(*pointer)
    

    将等同于

    while(49)
    

    因为1 的 ASCII 值为 49,并且条件被评估为 true

    这将一直持续到\0 递增九次后达到pointer 字符。

    现在,声明:

    while(*pointer)
    

    将等同于

    while(0)
    

    因为\0 的ASCII 值是0。因此,条件被评估为 false 并且循环停止。

    总结

    • while(condition) 中,condition 必须为非零才能继续循环执行。如果condition 计算结果为zero,则循环停止执行。

    • while(*pointer) 将一直有效,直到指向的内存位置的值是 non-zero ASCII 值。

    • 你也可以使用:

      if(*ptr){     //instead of if(*ptr!='\0')
        //do somthing
      }
      
      if(!*ptr){     //instead of if(*ptr=='\0')
        //do somthing
      }
      

    【讨论】:

      【解决方案3】:
      while(x) {
          do_something();
      }
      

      只要x 为真,就会重复运行do_something()。在 C 中,“真”表示“不为零”。

      '\0' 是一个空字符。在数值上为零(表示'\0' 的位与数字零相同;就像空格是数字 0x20 = 32)。

      所以你有while(*pointer != '\0')。虽然指向的内存不是零字节。前面我说“真”表示“非零”,所以比较x != 0(如果xintshort等)或x != '\0'(如果xchar ) 与 if、while 等中的 x 相同。

      您应该使用这种较短的形式吗?在我看来,没有。它使阅读代码的人不太清楚其意图是什么。如果你明确地写比较,它会让循环的意图更加明显,即使它们在技术上对编译器意味着同样的事情。

      因此,如果您编写 while(x)x 应该已经是一个布尔值或代表布尔值(真或假概念)的 C int。如果你写while(x != 0),那么你关心x是一个非零整数,并且正在用x做一些数字。如果你写while(x != '\0'),那么xchar,你想继续直到你找到一个空字符(你可能正在处理一个C 字符串)。

      【讨论】:

      • “非 0”答案是 OP 在这种情况下需要的正确答案,所以 +1。
      【解决方案4】:

      *pointer 正是它所说的:“给我存储在指针指向的位置的值”。或简称为“取消引用pointer”。在您的具体示例中,取消引用指针会产生字符串中的字符之一。

      while(*pointer) 也意味着确切的意思是:“当表达式*pointer 产生一个真值时,执行循环体”。

      由于 C 将所有非零值视为真,因此在条件中使用 *pointer 始终等同于使用表达式 *pointer != 0。因此,许多 C 程序员为了练习布尔禅而省略了!= 0 部分。

      【讨论】:

        【解决方案5】:

        让我们从一个简单的例子开始::

        int a = 2 ;
        int *b = &a ;
        /* Run the loop till *b i.e., 2 != 0 
        Now, you know that, the loop will run twice 
        and then the condition will become false 
        */
        while( *b != 0 )
        {
         *b-- ;
        }
        

        同样,您的代码正在使用char*,一个字符串。

        char var[10] ;
        /* copy some string of max char count = 9, 
        and append the end of string with a '\0' char.*/
        char *pointer = &var ;
        while( *pointer != '\0' )
        {
         // do something
         // Increment the pointer 1 or some other valid value
        }
        

        因此,while 循环将一直运行到 *pointer 不会命中 '\0'

        while( *pointer )
        /* The above statement means the same as while( *pointer != '\0' ),
        because, null char ('\0') = decimal value, numeric zero, 0*/
        

        但是当你这样做时,用法可能会改变,while(*pointer != 'x'), where x can be any char。在这种情况下,您的第一个代码将在 *pointer hits the 'x' char 之后退出,但您的第二个 sn-p 将运行到 *pointer hits '\0' char

        【讨论】:

          猜你喜欢
          • 2013-03-09
          • 1970-01-01
          • 2018-07-03
          • 2011-01-14
          • 2020-10-30
          • 2020-08-07
          • 2016-12-03
          • 2011-09-02
          • 1970-01-01
          相关资源
          最近更新 更多