【问题标题】:* Vs ++ precedence in C* C 中的 Vs ++ 优先级
【发布时间】:2012-11-12 05:18:11
【问题描述】:

我无法理解以下 C 代码的输出:

#include<stdio.h>
main()
{
   char * something = "something";
   printf("%c", *something++);  // s
   printf("%c", *something);    // o
   printf("%c", *++something);  // m
   printf("%c", *something++);  // m
}

请帮忙:)

【问题讨论】:

  • 为什么不包括输出呢?
  • 在编写这样的代码时,最好添加一些 () 以增加可读性,因为它更清楚您希望使用 *(something++) 或 (*something)++ 发生什么。
  • @Johan:是的,如果 ++ 或 * 更高,我总是要查找......而且经过 10 多年的 c++ ;-) 我总是这样做 *(++something)
  • 如果任何答案对你有帮助,你应该接受答案;-)

标签: c pointers


【解决方案1】:

详情请见http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

printf("%c", *something++);

在 *something 处获取字符,然后将其递增('s')

printf("%c", *something);

只需获取 char(现在是第二个,由于最后一条语句 ('o') 中的增量)

printf("%c", *++something);

递增然后获取新位置的字符('m')

printf("%c", *something++);

在 *something 处获取字符,然后将其递增 ('m')

【讨论】:

    【解决方案2】:

    这很简单。

    char * something = "something";
    

    指针赋值。

    printf("%c\n", *something++);//equivalent to *(something++)
    

    指针递增,但递增前的值被取消引用,因为它是后递增。

    printf("%c\n", *something);//equivalent to *(something)
    

    指针现在在前一个语句的增量后指向“o”。

    printf("%c\n", *++something);//equivalent to *(++something)
    

    指针递增指向“m”,并在递增指针后取消引用,因为这是预递增的。

    printf("%c\n", *something++);//equivalent to *(something++)
    

    与第一个答案相同。 还要注意 printf 中每个字符串末尾的 '\n'。它使输出缓冲区刷新并打印行。始终在 printf 的末尾使用\n

    您可能还想看看 this question

    【讨论】:

      【解决方案3】:
      // main entrypoint
      int main(int argc, char *argv[])
      {
          char * something = "something";
      
          // increment the value of something one type-width (char), then
          //  return the previous value it pointed to, to be used as the 
          //  input for printf.
          // result: print 's', something now points to 'o'.
          printf("%c", *something++);
      
          // print the characer at the address contained in pointer something
          // result: print 'o'
          printf("%c", *something);
      
          // increment the address value in pointer something by one type-width
          //  the type is char, so increase the address value by one byte. then
          //  print the character at the resulting address in pointer something.
          // result: something now points at 'm', print 'm'
          printf("%c", *++something);
      
          // increment the value of something one type-width (char), then
          //  return the previous value it pointed to, to be used as the 
          //  input for printf.
          // result: print 's', something now points to 'o'.
          printf("%c", *something++);
      }
      

      结果:

      somm
      

      【讨论】:

      • @MarioTheSpoon 太棒了名字,先生。
      【解决方案4】:

      始终使用顺时针规则clockwise rule

      printf("%c\n", *something++);
      

      根据规则你会首先遇到 * 所以获取值然后 ++ 表示递增

      第三种情况printf("%c\n", *something++);

      所以根据图像递增值++然后得到值*

      【讨论】:

      • 这是一个很好的参考,但它适用于声明中的类型,而不是表达式中的运算符。
      • 我在这种情况下尝试过,根据我上面给出的上述解决方案,它工作正常。但你是对的它的声明。如果答案具有误导性,我们深表歉意
      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2011-08-17
      相关资源
      最近更新 更多