0

*a = *b In C, the above expression is always interpreted as the assignment of the value at the address contained in b to the value at an address in a. But, according to the precedence of operators and associativity rule, * has higher priority than the assignment operator '=', and is associative from right to left. If we interpret the given expression keeping in mind these rules, it should be interpreted as:

*a=*b

(*a)=(*b)

10=20(Supposing *a=10 & *b=20)

So, this must result in an error if taken as per the rules of precedence and also as per the rule that there can't be an expression on the left-hand side of an assignment operator. Then why does this work?

1 Answers1

4

I'm not sure if this is in topic here.

Anyway, (*a) in (*a)=(*b) denotes a lvalue referring to the object in a. On the right side of the assignment, (*b) also denotes an lvalue, but this lvalue undergoes lvalue conversion and is converted to the actual value stored in the object pointed by $b$ (i.e., to 20).

In the end, the value 20 is assigned to the object pointed by a.

See, for example, this discussion about lvalues and rvalues.

Steven
  • 29,419
  • 2
  • 28
  • 49