名稱空間
變體
操作

NULL

來自 cppreference.com
< cpp‎ | 型別
 
 
 
型別支援
基本型別
定寬整數型別 (C++11)
定寬浮點型別 (C++23)
(C++11)    
(C++17)
(C++11)
NULL

數值極限
C 數值極限介面
執行時型別資訊
 
定義於標頭檔案 <clocale>
定義於標頭檔案 <cstddef>
定義於標頭檔案 <cstdio>
定義於標頭檔案 <cstdlib>
在標頭檔案 <cstring> 中定義
定義於標頭檔案 <ctime>
在標頭檔案 <cwchar> 中定義
#define NULL /* 實現定義 */

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