Skip to the content of the web site.

NULL vs 0 vs nullptr

If you are familiar with both C and C++, you may be familiar with the prevalence of NULL in C and 0 in C++ to represent null pointers (a reserved value indicating that the pointer does not store the address of a valid object).

In C, the macro NULL is cast as:

#define NULL ((void *) 0)

However, the original C++ standard is clear: the use of the zero address is preferable to using NULL in C++. It is both preferable and more correct to use it than the old NULL. To quote the author of the language, Bjarne Stroustrup in his text The C++ Programming Language:

In C it has become popular to define a macro NULL to represent the zero pointer. Because of C++'s tighter type checking, the use of plain 0, rather than any suggested NULL macro, leads to fewer problems. If you feel you must define NULL, use

    const int NULL = 0;

The const qualifier (ยง5.4) prevents accidental redefinition of NULL and ensures that NULL can be used where a constant is required."

In the C++-11 standard, they introduced a new representation of a null pointer, namely nullptr which is of type nullptr_t.

The benefit of this is that f(NULL) would call f(int) instead of f(void *) if f was so overloaded.