Contents |
nullptr | (since C23) | ||||||||
The keywordnullptr denotes a predefined null pointer constant. It is anon-lvalue of typenullptr_t.nullptr can beconverted to a pointer types orbool, where the result is the null pointer value of that type orfalse respectively.
Demonstrates that a copy ofnullptr can also be used as a null pointer constant.
#include <stddef.h>#include <stdio.h> void g(int*){puts("Function g called");} #define DETECT_NULL_POINTER_CONSTANT(e) \ _Generic(e, \ void* : puts("void*"), \ nullptr_t : puts("nullptr_t"), \ default : puts("integer") \ ) int main(){ g(nullptr);// OK g(NULL);// OK g(0);// OK auto cloned_nullptr= nullptr; g(cloned_nullptr);// OK [[maybe_unused]]auto cloned_NULL=NULL;// g(cloned_NULL); // implementation-defined: maybe OK [[maybe_unused]]auto cloned_zero=0;// g(cloned_zero); // Error DETECT_NULL_POINTER_CONSTANT(((void*)0)); DETECT_NULL_POINTER_CONSTANT(0); DETECT_NULL_POINTER_CONSTANT(nullptr); DETECT_NULL_POINTER_CONSTANT(NULL);// implementation-defined}
Possible output:
Function g calledFunction g calledFunction g calledFunction g calledvoid*integernullptr_tvoid*
| implementation-defined null pointer constant (macro constant)[edit] | |
(C23) | the type of the predefined null pointer constantnullptr (typedef)[edit] |
C++ documentation fornullptr | |