LWN.net Logo

NULL v. zero

NULL v. zero

Posted Jul 16, 2004 22:59 UTC (Fri) by jabcslwn (guest, #11815)
Parent article: NULL v. zero

I agree that NULL is not used enough for assignments, but throw another hat into the ring with the syntax
if(!someptr) instead of if(someptr == NULL) or
if(!someboolean) instead of if(someboolean==0).
After all, the C language values were designed to allow for this, thus
simplifying the language expression. Anybody want to shoot me down?


(Log in to post comments)

!x vs x == 0

Posted Jul 16, 2004 23:50 UTC (Fri) by giraffedata (subscriber, #1954) [Link]

I believe C was designed to allow "if (!a)" for the case that a is a logical (boolean) quantity. That it can be used with pointers is a byproduct. I believe the concept of the null pointer came later.

However, I accept "if (!a)" where a is a pointer, because a pointer which may have the null pointer value is in fact two pieces of information in one, and one of them is logical. a having a non-null-pointer value is the logical proposition that a exists. Looked at that way, "if (a == NULL)" is actually harder to read, and not because it uses a few more characters. Because it presents the absence-of-value NULL as if it were an actual pointer value.

Of course, one thing that will always raise my hackles is "if (!a)" where a is a number.

NULL v. zero

Posted Jul 17, 2004 4:33 UTC (Sat) by Ross (subscriber, #4065) [Link]

It's certainly allowed. If it is good style is another debate :)

Copyright © 2008, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds