Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone show an example where this precedence matters?

I'm reading precedence and associativity. In the table, I observed these two things -

(i) precedence of postfix increment(or decrement) operator is greater than precedence of prefix increment(or dec.) operator.

(ii) associativity of postfix inc.(or dec.) operator is left-to-right but that of prefix increment(or dec.) operator is right-to-left.

I'm not sure why it is needed. Can anyone help me by showing code(separately for each case), which shows the need of these two facts? Thanks.

I tried to think about cases but not getting any such(as I'm very new to programming).

like image 774
Ashis Ghosh Avatar asked Jan 01 '23 21:01

Ashis Ghosh


1 Answers

This is needed for an expression like

data = *pointer++;

You to obtain the value at pointer in data, and then increment it to the next element. If the precedence of the postfix weren't greater, you'd end up with an incremented value in data.

And the associativity of the prefix operator is right-to-left because you want an expression like

data = **pointer_to_pointer;

to be evaluated from right to left as if you'd write

data = *(*pointer_to_pointer);
like image 127
deets Avatar answered Jan 05 '23 17:01

deets