user4418808
user4418808

Reputation:

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 ?

Upvotes: 2

Views: 250

Answers (2)

Drew Dormann
Drew Dormann

Reputation: 63947

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.

Upvotes: 3

Gopi
Gopi

Reputation: 19874

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)

Upvotes: 1

Related Questions