In C, NULL
is a macro that expands to a null pointer constant.
7.19p3
The macros are
NULL which expands to an implementation-defined null pointer constant;
...
A null pointer constant is an integer constant expression with the value 0
(
e.g., 0
, 1-1
, 42*0LL
, etc.) or such an expression cast to (void*)
.
6.3.2.3p3
An integer constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.66) If a null
pointer constant is converted to a pointer type, the resulting
pointer, called a null pointer, is guaranteed to compare unequal to a
pointer to any object or function.
Most common C implementations define NULL
to be 0
, 0L
, or ((void*)0)
.
So you are correct. NULL
need not be a pointer.
(IIRC, C++ doesn't even allow the (void*)
cast in NULL
, meaning NULL
in C++ always has integer type. Because of that and because void*
pointers
do not compare with regular pointers so readily in C++, C++>=11 now has a special nullptr
keyword.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…