Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Predefined null pointer constant(since C23)

      From cppreference.com
      <c‎ |language
       
       
       
       

      Contents

      [edit]Syntax

      nullptr(since C23)

      [edit]Explanation

      The keywordnullptr denotes a predefined null pointer constant. It is anon-lvalue of typenullptr_t.nullptr can beconverted to a pointer types orbool, where the result is the null pointer value of that type orfalse respectively.

      [edit]Keywords

      nullptr

      [edit]Example

      Demonstrates that a copy ofnullptr can also be used as a null pointer constant.

      Run this code
      #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}

      Possible output:

      Function g calledFunction g calledFunction g calledFunction g calledvoid*integernullptr_tvoid*

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.4.4.6 Predefined constants (p: 66)

      [edit]See also

      implementation-defined null pointer constant
      (macro constant)[edit]
      the type of the predefined null pointer constantnullptr
      (typedef)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/nullptr&oldid=183159"

      [8]ページ先頭

      ©2009-2025 Movatter.jp