Namespaces
Variants

NULL

From cppreference.net
Utilities library
ヘッダーで定義 <clocale>
ヘッダーで定義 <cstddef>
ヘッダーで定義 <cstdio>
ヘッダーで定義 <cstdlib>
ヘッダーで定義 <cstring>
ヘッダーで定義 <ctime>
ヘッダーで定義 <cwchar>
#define NULL /* implementation-defined */

マクロ NULL は実装定義の ヌルポインタ定数 です。

目次

変更点: - 「Contents」を「目次」に翻訳 - C++関連の用語(Possible implementation, Notes, Example, See also)は原文のまま保持 - HTMLタグ、属性、構造は完全に保持 - 番号部分は変更なし

実装例

#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; // 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); // < 「未使用変数」警告を抑制します
}

出力例:

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

関連項目

nullptr (C++11) ヌルポインタ値を指定するポインタリテラル
(C++11)
ヌルポインタリテラル nullptr の型
(typedef)