命名空间
变体
操作

NULL

来自 cppreference.cn
< cpp‎ | types
 
 
 
 
定义于头文件 <clocale>
定义于头文件 <cstddef>
定义于头文件 <cstdio>
定义于头文件 <cstdlib>
定义于头文件 <cstring>
定义于头文件 <ctime>
定义于头文件 <cwchar>
#define NULL /* implementation-defined */

NULL 是一个实现定义的空指针常量

目录

[编辑] 可能的实现

#define NULL 0
// since C++11
#define NULL nullptr

[编辑] 注解

在 C 中,宏 NULL 可能具有 void* 类型,但这在 C++ 中是不允许的,因为空指针常量不能具有该类型。

[编辑] 示例

#include <cstddef>
#include <iostream>
#include <type_traits>
#include <typeinfo>
 
class S;
 
int main()
{
    int* p = NULL;
    int* p2 = static_cast<std::nullptr_t>(NULL);
    void(*f)(int) = NULL;
    int S::*mp = NULL;
    void(S::*mfp)(int) = NULL;
    auto nullvar = NULL; // may trigger a warning when compiling with gcc/clang
 
    std::cout << "The type of nullvar is " << typeid(nullvar).name() << '\n';
 
    if constexpr(std::is_same_v<decltype(NULL), std::nullptr_t>)
        std::cout << "NULL implemented with type std::nullptr_t\n";
    else
        std::cout << "NULL implemented using an integral type\n";
 
    [](...){}(p, p2, f, mp, mfp); // < suppresses "unused variable" warnings
}

可能的输出

The type of nullvar is long
NULL implemented using an integral type

[编辑] 参见

nullptr (C++11) 指定空指针值的指针字面量[编辑]
(C++11)
空指针字面量 nullptr 的类型
(typedef) [编辑]
C 文档 关于 NULL