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

Bill Carpenter wjc at ho5cad.ATT.COM
Thu Jul 20 03:26:02 AEST 1989


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");

Surely this "looks even better as the pointers get more complex" and
the whole expression would otherwise dance off the left edge of the
page?  Why make the code hard to read when there isn't even a payoff?
Is the point to show your pals in the Code Maintenance Garage that you
know all about the comma operator?  Or are you one of those poeple who
gets paid proportional to the inverse number of source lines?  :-)
--
   Bill Carpenter         att!ho5cad!wjc  or  attmail!bill



More information about the Comp.lang.c mailing list