[LC++]Null or 0
Torsten at Rennett.de
Mon Feb 23 17:15:01 UTC 2004
On Sonntag, 22. Februar 2004 16:12, Peter Poulsen wrote:
> I have read that we should use 0 over NULL when setting a pointer to
> Does anybody know why?
In C NULL is often defined as '(void *)0' and this is incorrect in C++
because there the type of NULL must be an integer type. Otherwise, you
could not assign NULL to a pointer. This is because in C++ there is no
automatic conversion from 'void *' to any other type.
So several people and style guides recommend not using NULL in C++. But
nowadays most of the heaeder files are prepared for using C++ and they
define NULL correctly (as 0, or with gcc as __null). Using NULL with gcc
is perfectly OK.
> To me it seems better to use NULL as it indicates
> in the code that this is a pointer being set to nothing, and not an
> interger being assigned the value 0.
ACK! And thats the reason why I still use NULL and also recommend in doing
> I am aware that in most compilers NULL is just a macro for 0.
... if you are using C++ (i.e. '__cplusplus' is defined). For C it will
most certainly still be '(void *)0'. Have a look at stddef.h.
Ingenieurbuero RENNETT -- innovative Software-Entwicklung --
Torsten Rennett http://www.RENNETT.de
Ludwig-Thoma-Weg 14 E-Mail: mailto:Torsten at Rennett.de
D-85551 Heimstetten Telefon: +49-89-90480538
More information about the tuxCPProgramming