*p++ = *p results?

Gregory Smith greg at utcsri.UUCP
Fri Mar 28 05:17:50 AEST 1986


In article <258 at necis.UUCP> yde at necis.UUCP (David Elins ext. 220) writes:
>In article <312 at imagen.UUCP> kevin at imagen.UUCP (Kevin L. Malloy) writes:
>>In the statement *p++ = *p; what will be the result? 

>My understanding, confirmed by lint, is that this is an ambiguous
>statement.  (lint said "warning: p evaluation order undefined")
>
...
>The real ambiguity is that C does not specify in which order the sides
>of an assignment operator (or any operator) get evaluated (c.f. K&R p. 49).
			       ^^^
The following operators have a defined order of execution:

a,b		first a, then b

a || b		eval a; if a==0 then b is evaluated

a && b		eval b; if a!=0 then b is evaluated

a ? b: c	eval a; if a!=0 then eval b else eval c.

This little point is very important, because much code depends on it.

This type of thing in particular is very common:

	if( p != NULL   &&   *p == What_I_am_looking_for )....

Evaluating *p will crash the program if p==NULL, but since the && operator
behaves the way it does, this will never happen.
-- 
"No eternal reward will forgive us now for wasting the dawn" -J. Morrison
----------------------------------------------------------------------
Greg Smith     University of Toronto       ..!decvax!utzoo!utcsri!greg



More information about the Comp.lang.c mailing list