Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      NULL

      From cppreference.com
      <c‎ |types
       
       
       
      Defined in header<locale.h>
      Defined in header<stddef.h>
      Defined in header<stdio.h>
      Defined in header<stdlib.h>
      Defined in header<string.h>
      Defined in header<time.h>
      Defined in header<wchar.h>
      #define NULL /*implementation-defined*/

      The macroNULL is an implementation-defined null pointer constant, which may be

      (since C23)

      A null pointer constant may beconverted to any pointer type; such conversion results in the null pointer value of that type.

      Contents

      [edit]Notes

      POSIX requiresNULL to be defined as an integer constant expression with the value0 cast tovoid*.

      [edit]Possible implementation

      // 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

      [edit]Example

      Run this code
      #include <inttypes.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h> int main(void){// any kind of pointer can be set to NULLint* 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 errorchar*ptr=malloc(0xFULL);if(ptr== NULL)printf("Out of memory");elseprintf("ptr = %#"PRIxPTR"\n",(uintptr_t)ptr);free(ptr);}

      Possible output:

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

      [edit]See also

      the type of the predefined null pointer constantnullptr
      (typedef)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/types/NULL&oldid=168678"

      [8]ページ先頭

      ©2009-2025 Movatter.jp