【问题标题】:Why *p++ dereferences a pointer first and then increments pointer adress?为什么 *p++ 先取消引用指针,然后增加指针地址?
【发布时间】:2019-12-25 15:14:38
【问题描述】:

C 运算符优先级图表如下所示:

为什么列表中的后自增/自减是第一个,而 *p++ 导致先取消引用一个指针,然后再增加它指向的地址?

【问题讨论】:

  • 运算符优先级与求值顺序不同。

标签: c operators operator-precedence


【解决方案1】:

*p++ 解析为*(p++)

p++ 的值是p(副作用是递增p),所以*p++ 的值与*p 的值相同。

除了副作用之外,*p*p++ 是相同的。

整数也会发生同样的事情:

int n = 6;
printf("%d\n", 7 * n);
printf("%d\n", 7 * n++); // except for the side-effect same value as above

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2012-01-02
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2023-01-16
    相关资源
    最近更新 更多