c - C 中的求值顺序(对于带 * 和++ 的表达式)

标签 c operator-precedence postfix-operator

<分区>

如果我们有

int i = 5; /*line 1*/
int *p = &i; /*line 2*/
*p++; /*line 3*/ 

第 3 行的评估顺序是什么? (我知道在这个 p 之后可能会指向一些奇怪的东西 - 但我对正确的内存寻址不感兴趣,而是评估的顺序,因为 * 和++ 似乎具有相同的优先级)

最佳答案

不,postfix increment has higher precedence .

在您的情况下,增量的副作用将在值评估之后发生 note (使用取消引用运算符)。但是,值评估的结果会被丢弃(您没有做任何努力来存储结果)。

因此,最后,结果将是 等同于 p++;


注意:

引用 C11,第 6.5.2.4 章,(强调我的)

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The value computation of the result is sequenced before the side effect of updating the stored value of the operand. [...]

关于c - C 中的求值顺序(对于带 * 和++ 的表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43868051/

相关文章:

c++ - 为什么后缀增量运算符采用虚拟参数?

c++ - 使用堆栈和迭代器编写后缀计算器

c++ - C 系统调用 pipe、fork 和 execl

c - 内存位置和范围

c - 为什么我没有得到任何输出

c++ - C中函数执行的优先顺序

PHP 求幂运算符优先级

java - preincrement postincrement 优先级问题

c - do while not execution, 意外终止

c++ - 取消引用和后缀++优先级