use of if (!cptr) and if (cptr) && a programming error

Norman Diamond diamond at csl.sony.JUNET
Mon Jul 24 12:08:09 AEST 1989


Someone suggested:
>>>        while ( ( myptr = my_func() )->x != myptr->y )

In article <2990 at nmtsun.nmt.edu> dwho at nmtsun.nmt.edu (David Olix) writes:

>>THAT WILL NOT WORK! [...]

Many people argued that it should work, because of the parentheses.  (I
almost did too, but continued reading first to see if anyone else did.)

In article <18677 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:

>Or, more precisely, is not guaranteed.
[...]
>The pANS makes some limited constraints, but not enough to warrant the
>conclusion [that it will work].
>
>In short, whether `myptr->y' is obtained before or after `(fncall)->x'
>is not defined in C, and different implementations do in fact have
>different evaluation order.

(Thanks to Chris Torek for closing my mouth before I put my foot in it.)

But!  What about the following famous idiom!

    int ch;  /* everyone knows it cannot be char */
    while ((ch = getchar()) != EOF) { ... }

The old value of ch might be compared to EOF?  The loop might be
executed in incorrect cases (and might not be executed sometimes when
it should be)?
IS THIS REALLY TRUE ??????????????????
Are there really millions of broken C programs due to this idiom?

--
-- 
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.jp at relay.cs.net)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.



More information about the Comp.lang.c mailing list