*p++ = *p and more
john at wvlpdp
john at wvlpdp
Fri Apr 18 02:26:00 AEST 1986
K & R says on the last page (50) of Chapter 2.
Function calls, nested assignment statements and increments and
decrement operators cause "side effects" - some variable is changed
as a byproduct of the evaluation. In any expression involving side
effects, there can be subtle dependencies on the order in which
variables taking part in the expression are stored. One unhappy
situation is typified by the statement:
a[i] = i++;
The question is whether the subscript is the old value of i or the new.
The compiler can do this in different ways, and generate different
answers depending on its interpretation. When side effect (assignment
to actual variables) takes place is left to the discretion of the
compiler, since the best order strongly depends on machine architeture.
The moral of this discussion is that writing code which depends on
order of evaluation is a bad programming practice in any language.
Naturally, it is necessary to know what things to avoid, but if you
don't know how they are done on various machines, that innocence may
help to protect you. (The C verifier lint will detect most dependencies
on order of evaluation.)
More information about the Comp.lang.c
mailing list