<分区>
如果我们有
int i = 5; /*line 1*/
int *p = &i; /*line 2*/
*p++; /*line 3*/
第 3 行的评估顺序是什么? (我知道在这个 p 之后可能会指向一些奇怪的东西 - 但我对正确的内存寻址不感兴趣,而是评估的顺序,因为 * 和++ 似乎具有相同的优先级)
<分区>
如果我们有
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/