Defined in header <stddef.h> | ||
typedef typeof(nullptr) nullptr_t; | (since C23) | |
nullptr_t is the type of the predefined null pointer constant,nullptr. It is a distinct type that is not itself a pointer type. It can beimplicitly converted to any pointer type orbool, and the result is the null pointer value of that type orfalse respectively. No type other thannullptr_t itself can be converted or explicitly cast tonullptr_t.
sizeof(nullptr_t) andalignof(nullptr_t) are equal tosizeof(void*) andalignof(void*) respectively.
nullptr_t has only one valid value, i.e.,nullptr. The object representation ofnullptr is same as that of(void*)0. If anlvalue conversion produces anullptr_t value with a different object representation, the behavior is undefined.
Demonstrate thatnullptr_t is a distinct type.
#include <stddef.h>#include <stdio.h> #define DETECT_NULL_POINTER_CONSTANT(e) \ _Generic(e, \ void* : puts("void*"), \ nullptr_t : puts("nullptr_t"), \ default : puts("other") \ ) int main(){ DETECT_NULL_POINTER_CONSTANT(((void*)0)); DETECT_NULL_POINTER_CONSTANT(0); DETECT_NULL_POINTER_CONSTANT(nullptr);}
Output:
void*othernullptr_t
| implementation-defined null pointer constant (macro constant)[edit] | |
C++ documentation fornullptr_t | |