相同运算符优先级的结合性 -- *start++ [英] Associativity of the same operator precedence -- *start++

查看:64
本文介绍了相同运算符优先级的结合性 -- *start++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现以下表达式:

Why does the following expression:

total += *start++;

评估:

total += (*start)++;

而不是:

total += *(start++); // though this doesn't really matter either it would be the same

++(后缀增量)和 *(解引用具有相同的优先级并从右到左关联,那么为什么 不是++ 后缀先求值?

The ++ (postfix increment) and * (dereference have the same precedence and associate from right-to-left, so why isn't the ++ postfix evaluated first?

或者,后缀是否在序列点之后求值,等等:

Or, does the postfix evaluate after a sequence point, and so:

total += *++start

将评估为:

total += *(++start)

但是因为后缀发生在:

total += *start++

将评估为:

total += (*start)++;

换句话说,从右到左结合性在上面的表达式中并不重要,因为在表达式求值过程中,即使是 post-fix 也是在 not 之后求值的?

In other words, Right-to-Left associativity is not important in the above expression, because even post-fix is evaluated after not during the expression evaluation?

推荐答案

后缀++运算符确实比解引用运算符*具有更高的优先级代码>.所以表达式解析为:

The postfix ++ operator does have higher precedence than the dereference operator *. So the expression parses as:

total += *(start++);

您可能会感到困惑的是,后缀 ++ 运算符的结果是操作数 before 递增.实际增量是表达式的无序副作用.

What might be confusing you is that the result of the postfix ++ operator is the operand before it is incremented. The actual increment happens as an unsequenced side effect of the expression.

所以这个表达式取start 的原始值,取消引用它,并将该值添加到total.到表达式完全求值时,start 递增.

So this expression takes the original value of start, dereferences it, and adds that value to total. By the time the expression is fully evaluated, start is incremented.

请注意,这不同于:

total += (*start)++;

因为这会增加 start 指向的内容而不是 start 本身.

Because this increments what start points to instead of start itself.

这篇关于相同运算符优先级的结合性 -- *start++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆