use of if (!cptr) and if (cptr), where cptr is a *

Erik Murrey erik at mpx2.mpx.com
Fri Jul 21 05:12:45 AEST 1989


In article <WJC.89Jul19182602 at ho5cad.ho5cad.ATT.COM> wjc at ho5cad.ATT.COM (Bill Carpenter) writes:
>In article <10100 at mpx2.mpx.com> erik at mpx2.mpx.com (Erik Murrey) writes:
>> 	struct xyzzy	*sptr;
>> 	if (sptr= my_function(), sptr->x == 0)
>> 		printf("x is zero\n");
>> 
>> I think the second is much clearer.  This looks even better as the 
>> pointers get more complex, since the pointer operators go right
>> on the ptr itself, and not on an expression.
>
>Since there was no indication that this was intended as a joke ...
>
>I'll admit that this cloudy fragment looks clearer than the other
>one you cited, but what the heck is wrong with (besides not checking
>"sptr" being null):
>
>	struct xyzzy	*sptr;
>	sptr= my_function();
>	if (sptr->x == 0)
>		printf("x is zero\n");


Maybe its hard for you to read, but I'm used to it.

Perhaps my example was bad.  The above coding method is
even more useful in while() and for() loops:

while (cc= fgetc(fp), cc != '\n') {
	/* process more of line */
	...
}


or even:

while (myptr= my_func(), myptr->x != myptr->y) {
	/* ... */
	...
}

I would kinda like to see that one in your style...




... Erik


	-- 
Erik Murrey                            /|   //  /~~~~/  |  /
MPX Data Systems, Inc.                / | / /  /____/   |/
erik at mpx.com                         /  /  /  /        /|  Data Systems, Inc. 
{vu-vlsi, bpa, cbmvax}!mpx1!erik    /     /  /       /  |====================



More information about the Comp.lang.c mailing list