简体   繁体   中英

Operator precedence Confusion

Hello guys after going through href= http://en.cppreference.com/w/c/language/operator_precedence this link , I thought I understood the operator precedence but I came to followings doubt. The link says that When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expression *p++ is parsed as *(p++), and not as (*p)++.
So how does the expression ++*p gets evaluated is it like ++(*p) but if yes ++ has higher priority or bound then *, Then why does * is bound tighter in the above case, and what about the expression *++p ?

Operator precedence defines which operator should be applied first when there is more than one choice .

From your link:

Precedence and associativity are independent from order of evaluation .

The expression ++*p , or any expression of the form:

{operator 2} {operator 1} {expression}

has a well-defined order of evaluation , where {operator 1} {expression} must be applied in order to make an expression that {operator 2} may act on.

Yes ++ has higher precedence over * and the associativity for both is from right to left.

So

++*p will be evaluated as ++(*p) because ++ need to be applied on a modifiable value.

Whereas

*++p as you see when this is evaluated the operator close to p is ++ as well as have higher precedence over * so ++p will happen first followed by dereferencing *(++p)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM