LWN.net Logo

NULL v. zero

NULL v. zero

Posted Jul 15, 2004 19:40 UTC (Thu) by Ross (subscriber, #4065)
In reply to: NULL v. zero by ikm
Parent article: NULL v. zero

Is it not strange for the language to use the lhs to determine how to
evaluate the rhs? This always bothered me about C++. For example:

int bob;
void *nancy;

bob = NULL*NULL; // NULL is an integer here but a warning would be nicer

nancy = NULL*5; // So you say NULL is a pointer here but I don't actually believe it


(Log in to post comments)

NULL v. zero

Posted Jul 15, 2004 22:02 UTC (Thu) by sir99 (guest, #3286) [Link]

If NULL is defined as (void*)0, then neither of those is legal C++. It seems that gcc special-cases NULL so that it can be treated as both an integer and a pointer. Further, only an expression that evaluates to 0 at compile-time can be implicitly cast to a pointer; no other integer can be.

AIUI, the lhs doesn't determine how the rhs is evaluated. The rhs is evaluated independently and then promoted to the type of the lhs.

NULL v. zero

Posted Jul 16, 2004 19:35 UTC (Fri) by Ross (subscriber, #4065) [Link]

"AIUI, the lhs doesn't determine how the rhs is evaluated. The rhs is evaluated independently and then promoted to the type of the lhs."

That was my point, really. That's why NULL as (void *)0 is better than just
zero, and why C++'s insistance that 0 is better confuses me.

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