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

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Jul 18 01:34:44 AEST 1989


In article <10099 at mpx2.mpx.com> erik at mpx2.mpx.com (Erik Murrey) writes:
>Would (!sptr) break anywhere?  Should it be discouraged?

When sptr is a pointer variable, the following are equivalent:
	if(!sptr)
	if(sptr==0)
	if(sptr==NULL)	/* assuming NULL is properly defined */
There is no need to cast the 0 (or NULL) to the type of sptr for this
comparison.

A common idiom is
	if ( (sptr = (type *)malloc( size )) == NULL )



More information about the Comp.lang.c mailing list