[英]Associativity of the same operator precedence — *start++
為什么會出現以下表達式:
total += *start++;
評估為:
total += (*start)++;
並不是:
total += *(start++); // though this doesn't really matter either it would be the same
++
(后綴增量)和*
(取消引用具有相同的優先級並且從右到左關聯,那么為什么不首先評估++
后綴呢?
或者,后綴是否在序列點之后進行評估,所以:
total += *++start
將評估為:
total += *(++start)
但是因為后綴發生在:
total += *start++
將評估為:
total += (*start)++;
換句話說,Right-to-Left 關聯性在上述表達式中並不重要,因為即使是 post-fix 在 not 在表達式評估期間進行評估?
后綴++
運算符的優先級確實高於取消引用運算符*
。 所以表達式解析為:
total += *(start++);
可能讓您感到困惑的是,后綴++
運算符的結果是它被遞增之前的操作數。 實際的增量作為表達式的未排序副作用發生。
所以這個表達式獲取start
的原始值,取消引用它,並將該值添加到total
。 當表達式被完全計算時, start
遞增。
請注意,這不同於:
total += (*start)++;
因為這會增加start
指向的內容而不是start
本身。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.