名前空間
変種
操作

NULL

From cppreference.com
< C‎ |
ヘッダ <locale.h> で定義
ヘッダー <stddef.h> で定義
ヘッダー <stdio.h> で定義
ヘッダー <stdlib.h> で定義
ヘッダー <string.h> で定義
ヘッダー <time.h> で定義
ヘッダー <wchar.h> で定義
#define NULL /*実装依存*/

マクロNULLは実装依存のヌルポインタ定数であり、以下のいずれかです。

  • 事前定義された定数 nullptr
(C23以降)

ヌルポインタ定数は、任意のポインタ型に変換できます。このような変換の結果は、その型のヌルポインタ値になります。

目次

[編集] 注記

POSIXではNULLは、値が0void*にキャストされた整数定数式として定義されることが要求されています。

[編集] 実装例

// C++ compatible:
#define NULL 0
// C++ incompatible:
#define NULL (10*2 - 20)
#define NULL ((void*)0)
// since C23 (compatible with C++11 and later)
#define NULL nullptr

[編集]

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    // any kind of pointer can be set to NULL
    int* p = NULL;
    struct S *s = NULL;
    void(*f)(int, double) = NULL;
    printf("%p %p %p\n", (void*)p, (void*)s, (void*)(long)f);
 
    // many pointer-returning functions use null pointers to indicate error
    char *ptr = malloc(0xFULL);
    if (ptr == NULL)
        printf("Out of memory");
    else
        printf("ptr = %#" PRIxPTR"\n", (uintptr_t)ptr);
    free(ptr);
}

実行結果の例

(nil) (nil) (nil)
ptr = 0xc001cafe

[編集] 関連

事前定義されたヌルポインター定数 nullptr の型
(typedef) [編集]
English 日本語 中文(简体) 中文(繁體)