Search code examples
cpointersstructureoperator-precedence

What is the order of evaluation in below code written in C?


a is a pointer to structure and b is the structure

a++ -> b

The order of evalution of above code will be

((a++) -> b)

or

(a -> b) ++

Solution

  • As postfix increment and the pointer to member operator have exactly the same precedence, the associativity of the two operators comes into play. Both are left to right.

    Hence

    a++ -> b
    

    is evaluated as

    (a++) -> b
    

    This means that the -> applies to the previous value of the pointer a, and a is incremented with the normal rules - to be accomplished sometime before the statement completes execution.

    (One for the pub quiz, the prefix increment has a lower precedence than ->).