Remember precedence rules: (was Re: use of if (!cptr))

ron.mackey mackey at cbnewsc.ATT.COM
Tue Jul 25 09:37:37 AEST 1989


In article <9042 at chinet.chi.il.us>, les at chinet.chi.il.us(Leslie Mikesell)writes:
> In article <10103 at mpx2.mpx.com> erik at mpx2.mpx.com (Erik Murrey) writes:
> 
> >while (myptr= my_func(), myptr->x != myptr->y) {
> >	...
> I'd use: 
> while (myptr = my_func() && myptr->x != myptr->y) {
>
> which would detect a null pointer returned by my_func() as well as
> providing a guaranteed order of evaluation.  

It would also be incorrect since "&&" binds tighter than "="
Your expression would be evaluated as follows:

	while (myptr = (my_func() && (myptr->x != myptr->y))) {

The order of evaluation is as follows:

-----------------------------------------------
  	evaluate my_func() 		

  	if (my_func() returns 0) 
	then 
		assign 0 to myptr
	else
		evaluate (myptr->x != myptr->y)

		if (myptr->x != myptr->y) 
		then
			assign 1 to myptr
		else
			assign 0 to myptr
-----------------------------------------------

Not exactly what the original poster requested, eh?

Ron Mackey			ihlpb!mackey
AT&T Bell Laboratories		(312) 979-2140



More information about the Comp.lang.c mailing list