Defined in header <clocale> | ||
---|---|---|
Defined in header <cstddef> | ||
Defined in header <cstdio> | ||
Defined in header <cstdlib> | ||
Defined in header <cstring> | ||
Defined in header <ctime> | ||
Defined in header <cwchar> | ||
#define NULL /*implementation-defined*/ |
The macro NULL
is an implementation-defined null pointer constant, which may be.
an integral constant expression rvalue of integer type that evaluates to zero. | (until C++11) |
an integer literal with value zero, or a prvalue of type | (since C++11) |
A null pointer constant may be implicitly converted to any pointer and pointer to member type; such conversion results in the null pointer value of that type. If a null pointer constant has integer type, it may be converted to a prvalue of type std::nullptr_t
.
#define NULL 0 //since C++11 #define NULL nullptr |
In C, the macro NULL
may have the type void*
, but that is not allowed in C++.
Some implementations define NULL
as the compiler extension __null
with following properties:
__null
is equivalent to a zero-valued integer literal (and thus compatible with the C++ standard) and has the same size as void*
, e.g. it is equivalent to 0
/0L
on ILP32/LP64 platforms respectively; __null
to an arithmetic type, including the type of __null
itself, may trigger a warning. #include <cstddef> #include <type_traits> #include <iostream> #include <typeinfo> class S; int main() { int* p = NULL; int* p2 = static_cast<std::nullptr_t>(NULL); void(*f)(int) = NULL; int S::*mp = NULL; void(S::*mfp)(int) = NULL; auto nullvar = NULL; // a warning may be triggered when compiling with gcc/clang std::cout << "The type of `nullvar` is " << typeid(nullvar).name() << '\n'; if constexpr(std::is_same_v<decltype(NULL), std::nullptr_t>) { std::cout << "NULL implemented with type std::nullptr_t\n"; } else { std::cout << "NULL implemented using an integral type\n"; } [](...){}(p, p2, f, mp, mfp); //< suppresses "unused variable" warnings }
Possible output:
The type of `nullvar` is long NULL implemented using an integral type
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 903 | C++11 | constant expressions with zero value such as 1-1 were allowed | only the literal zero is allowed |
nullptr (C++11) | the pointer literal which specifies a null pointer value |
(C++11) | the type of the null pointer literal nullptr (typedef) |
C documentation for NULL |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/NULL