所以,我之前和我的教授争论过,为 NULL 不是指针辩护,但他坚持认为这是因为存在 NULL 指针这样的东西。所以,如果 NULL 真的是一个指针,我现在有点困惑
我已经尝试在互联网上搜索但找不到任何答案,所以我最后的选择是这里
最佳答案
在 C 中,NULL
是一个扩展为空指针常量的宏。
The macros are
NULL which expands to an implementation-defined null pointer constant; ...
空指针常量是一个整型常量表达式,其值为0
(
例如,0
、1-1
、42*0LL
等)或此类表达式转换为 (void*)
.
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.
最常见的 C 实现将 NULL
定义为 0
、0L
或 ((void*)0)
.
所以你是是对的。 NULL
不必是指针。
(IIRC,C++ 甚至不允许 (void*)
强制转换为 NULL
,这意味着 C++ 中的 NULL
始终具有整数类型. 因为那个和因为 void*
指针
不要轻易在 C++ 中与常规指针进行比较,C++>=11 现在有一个特殊的 nullptr
关键字。)
关于c - NULL 是指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55520687/