Not only that, but...

Gregory Smith greg at utcsri.UUCP
Sun Nov 30 06:07:05 AEST 1986


In article <18 at houligan.UUCP> dave at murphy.UUCP (Rael's brother John) writes:
[ in another article he writes ...]
>>>Not only that, but: don't even *USE* an assigned-to object anywhere else
>>>in the expression!

well, that's it for 'i=i+1;', i guess.....	:-)
Somebody else:
>>
>>There are operators in C that are guaranteed to
>>preserve order, so you can make an assignment then use the variable
>>assigned to.
... stuff about &&, ||, ',' etc.

>You're right.  Just to prove that I'm not dogmatic about it, I will reveal
>an idiom of my own the violates my own assertion (the side effect involved
>here is a function call rather than an assignment, but the principle is
>the same):
>
>       while ( (fgets(buffer,sizeof(buffer),foo) , !feof(foo) )
...
>I can think of one other case that seems like it should work, although I can't
>prove it:
>
>	j = ( (k=func())>0 ? k : 0)
>
>The intent is that func is called and assigned to k, and then the value
>assigned to j is either the value of k if k>0, or 0 if k<=0.  This seems
>that it should be OK, because K&R states that for the question-mark
>operator, only one of the second and third operands is evaluated, and
>it seems to me that the first operand would first have to be fully evaluated in
>order to determine which of the other two to evaluate.  But I can't find
>any place where it comes right out and says this.

I think you just proved it.
Both of these are guaranteed to work as you say. It's not as simple
and restrictive as saying "you can't use an assigned to object anywhere
else in the expression", but it's not all that complicated, either.
-- 
----------------------------------------------------------------------
Greg Smith     University of Toronto      UUCP: ..utzoo!utcsri!greg
Have vAX, will hack...



More information about the Comp.lang.c mailing list