From: "Dave Dunfield" <dave06a at
dunfield.com>
N = 1;
A[N++] = N++;
I'd expect that to evaluate to a[1]=2 and n winds up being 3. But I'm
weird.
This simple code snippet invokes "undefined behaviour" because:
'N' is modified twice within a single sequence block.
'N' is referenced within a sequence block separately from a modification
occuring within the same sequence block.
One might expect:
N = 1
Save address of A[1]
++N
(address of A[1]) = 2
++N
Which gives you the result you expected, however a compiler is free to
perform the side effects occuring within a sequence block at any time it
wishes, as long as it observes the charactistics of the operator causing
the side effect. (ie: one instance of N++ cannot increment N before
retrieving it's value, although the other instance of N++ could have
incremented it).
Hi Dave
I'd expect it to evaluate the left side of the equation first before
determining the address. A[2]=1. Any other order would not make
sense. Algebraic equations do not read left to right. This causes
a sense of ambiguity but shouldn't. Where it wouldn't be clear
as to the result would be for the case:
A = (5*N++) + (6*N++)
But that only goes to show how poor this type of notation is for
defining sequence of execution. This is why I still think RPN notation
is much better for computer programming. This need not be a stack
language like Forth. It is concise. Events happen left to right. No special
rules. It isn't familar to what you learned in grade school but doesn't
take long to grasp.
Dwight
_________________________________________________________________
PC Magazine?s 2007 editors? choice for best Web mail?award-winning Windows
Live Hotmail.