If I remember correctly most C compilers define NULL like this:
#define NULL ((void*)0)
This is to ensure that NULL is interpreted as being a pointer type (in C). However this can cause issues in the much more type strict world of C++. Eg:
// Example taken from wikibooks.org
std::string * str = NULL; // Can't automatically cast void * to std::string *
void (C::*pmf) () = &C::func;
if (pmf == NULL) {} // Can't automatically cast from void * to pointer to member function.
Therefore in the current C++ standard null pointers should be initialized with the literal 0. Obviously because people are so used to using the NULL define I think a lot of C++ compilers either silently ignore the issue or redefine NULL to be 0 in C++ code. Eg:
#ifdef __cplusplus
#define NULL (0)
#else
#define NULL ((void*)0)
#endif
The C++x0 standard now defines a nullptr
keyword to represent null pointers. Visual C++ 2005's CLI/C++ compiler also uses this keyword when setting managed pointers to null. In current compilers you can create a template to emulate this new keyword.
There is a much more detailed article on wikibooks.org discussing this issue.
char *x = NULL;
char *x = 0;
char x = 0;
char *x = '\0';
char x = '\0';
– EvilTeach Oct 21 '08 at 01:51