I'm writing a stack implementation in C. The stack structure contains an int 'top', which contains the index of the value at the top of the stack. Therefore, to push something to the stack, it makes sense to use something like stack[++top] = new_value
. I see this pattern of incrementing a variable in an array subscript all the time, and it saves a line of code.
However, this C style guide has this to say about the above code:
Do not use expressions with side-effects in complicated ways. Do not assume that
the compiler evaluates things between sequence points in a particular order.
a[i++] = i; /* undefined behaviour! */
Is this really true? And, if it is, why? Since a lot of code uses this pattern, I would assume most modern compilers support it, which means it should've been adopted into a standard at some point. After all, if you really shouldn't use it, what's the point of differentiating between ++i
and i++
?