powerful expressions

Richard H. Gumpertz rhg at cpsolv.UUCP
Sat Dec 9 05:57:34 AEST 1989


In article <5040 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
>In article <24735 at cup.portal.com> Tim_N_Roberts at cup.portal.com writes:
>>In <12855 at cit-vax.Caltech.Edu>, wen-king at cit-vax.Caltech.Edu (King Su) shares:
>>>	((qhead) ? (qtail = qtail->next = qnew)
>>>		 : (qtail = qhead       = qnew))->next = 0;
...
>Fear no more.
>
>The assignment operators group right-to-left.  That is
>
>	qtail->next = qnew
>
>is performed before
>
>	qtail = qtail->next
>
>is performed.

X3J11/88-158, page 10, line 22: "The grouping of an expression does not
completely determine its evaluation."

X3J11/88-158, page 39, line 5: "Between the previous and next sequence
point an object shall have its stored value modified at most once by the
evaluation of an expression.  Furthermore, the prior value shall be
accessed only to determine the value to be stored."

X3J11/88-158, page 53, line 30: "The side effect of updating the stored
value of the left operand [of an assignment operator] shall occur
between the previous and the next sequence point."

X3J11/88-158, page 53, line 33: "The order of evaluation of the operands
[of an assignment operator] is unspecified."

Much as I would like to agree that the code is safe, I can't find anything
in the standard that guarantees it.  I think the standards committee may
have (intentionally?) blown this one; can anyone prove me wrong?

The following should always work (assuming NULL is defined by the
appropriate #include):
   (qhead ? qtail->next = qnew : qhead = qnew), (qtail = qnew)->next = NULL;

By the way, I would like to have written
   (qhead ? qtail->next : qhead) = qnew, (qtail = qnew)->next = NULL;
but lvalues are converted to rvalues in this context so THIS WON'T WORK!

-- 
===============================================================================
| Richard H. Gumpertz rhg%cpsolv at uunet.uu.NET -or- ...uunet!amgraf!cpsolv!rhg |
| Computer Problem Solving, 8905 Mohawk Lane, Leawood, Kansas 66206-1749      |
===============================================================================



More information about the Comp.lang.c mailing list