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

Glen Ecklund glen at astroatc.UUCP
Thu Aug 3 00:15:52 AEST 1989


In article <10592 at riks.csl.sony.JUNET> diamond at riks. (Norman Diamond) writes:
->Someone suggested:
->>>>        while ( ( myptr = my_func() )->x != myptr->y )
->
->In article <18677 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
->>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.
->
->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?

Nope.  Note that in the first example, the problem is not that the left
use of myptr might get the wrong value, it is guaranteed to get the value
returned by my_func().  The problem is that the right use of myptr could
get either.

The value of the expression (ch = getchar()) will similarly always be
the value returned by getchar().  No problem.

Glen



More information about the Comp.lang.c mailing list