Namespaces
Variants

Predefined null pointer constant (since C23)

From cppreference.net

목차

구문

nullptr (C23부터)

설명

키워드 nullptr 는 미리 정의된 null 포인터 상수를 나타냅니다. 이것은 비左값 으로서 nullptr_t 타입을 가집니다. nullptr 변환 되어 포인터 타입이나 bool 이 될 수 있으며, 그 결과는 각각 해당 타입의 null 포인터 값이나 false 가 됩니다.

키워드

nullptr

예제

nullptr 의 복사본도 널 포인터 상수로 사용될 수 있음을 보여줍니다.

#include <stddef.h>
#include <stdio.h>
void g(int*)
{
    puts("Function g called");
}
#define DETECT_NULL_POINTER_CONSTANT(e) \
    _Generic(e,                         \
        void* : puts("void*"),          \
        nullptr_t : puts("nullptr_t"),  \
        default : puts("integer")       \
    )
int main()
{
    g(nullptr); // OK
    g(NULL); // OK
    g(0); // OK
    auto cloned_nullptr = nullptr;
    g(cloned_nullptr); // OK
    [[maybe_unused]] auto cloned_NULL = NULL;
//  g(cloned_NULL); // implementation-defined: maybe OK
    [[maybe_unused]] auto cloned_zero = 0;
//  g(cloned_zero); // Error
    DETECT_NULL_POINTER_CONSTANT(((void*)0));
    DETECT_NULL_POINTER_CONSTANT(0);
    DETECT_NULL_POINTER_CONSTANT(nullptr);
    DETECT_NULL_POINTER_CONSTANT(NULL); // implementation-defined
}

가능한 출력:

Function g called
Function g called
Function g called
Function g called
void*
integer
nullptr_t
void*

참고문헌

  • C23 표준 (ISO/IEC 9899:2024):
  • 6.4.4.6 사전 정의된 상수 (p: 66)

참고 항목

구현에서 정의된 널 포인터 상수
(매크로 상수)
미리 정의된 널 포인터 상수 nullptr 의 타입
(typedef)