use of if (!cptr) and if (cptr) && a programming error
Ari Halberstadt
ari at eleazar.dartmouth.edu
Sat Jul 22 07:37:43 AEST 1989
In article <2990 at nmtsun.nmt.edu> dwho at nmtsun.nmt.edu (David Olix) writes:
#>[Skipping all the "In article..." stuff]
#>>[Material everybody has seen n times deleted.]
#>>I am kind of butting in [ :-) ], but how 'bout
#>> while ( ( myptr = my_func() )->x != myptr->y )
#>> {
#>> }
#>
#>>THAT WILL NOT WORK! [...]
#>
#>Actually, "Mastering C" by Craig Bolon, SYBEX Inc., p. 273 says that
#>parenthesis operators have the HIGHEST priority, therefore what's inside
#>(myptr = my_func()) would get evaluated first. Also, it specifies that
#>grouping for the '!=' operator is from left to right. Now, the author of
#>this book may have been wrong.... Anyone seen an "official" statement
#>from K&R?
True, parenthesis have the highest priority, but that is not relavent.
Syntactically, what you have is:
while (expr1 != expr2) { ... }
Since the two expressions are totally separate, the compiler is free
to do whatever it chooses. The expressions become a single expression when
(and only when) the compiler evaluates the != operator. The whole reason
for not defining order of evaluation was to allow machines to evaluate things
in the fastest way.
Grouping is not the same as order of evaluation...it's been a while since
I've gone over that, and I don't have a book in front of me right now.
The ANSI C standard has formalized all these things, but again, I don't
have a copy in front of me.
-- Ari Halberstadt '91, "Long live succinct signatures"
E-mail: ari at eleazar.dartmouth.edu Telephone: (603)640-5687
Mailing address: HB1128, Dartmouth College, Hanover NH 03755
More information about the Comp.lang.c
mailing list