nullptr 常量

版本新特性 / C23

// *** before: ***
//NULL is a macro, 0 or ((void *)0) as each library decides, so its
//type is a guess; in a variadic call a bare 0 goes as an int
char *nameOld = NULL;
const char *nullKindOld = _Generic(NULL, int: "int", long: "long",
                                   void *: "void *", default: "other");
//nullKindOld is "void *" with this library, "int" with another

printf("nullKindOld is %s\n", nullKindOld);

// *** in version C23: ***
//nullptr is a keyword with a type of its own, nullptr_t;
//it turns into any pointer and into bool, never into a number
char *name = nullptr;
const char *nullKind = _Generic(nullptr, typeof(nullptr): "nullptr_t",
                                default: "other");
bool empty = (name == nullptr);
//nullKind is "nullptr_t"
//empty is true

//int n = nullptr;    <- error: nullptr is not a number
printf("nullKind is %s\n", nullKind);
printf("empty is %s\n", empty ? "true" : "false");