From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/types%2Fnull.html | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 devdocs/c/types%2Fnull.html (limited to 'devdocs/c/types%2Fnull.html') diff --git a/devdocs/c/types%2Fnull.html b/devdocs/c/types%2Fnull.html new file mode 100644 index 00000000..fc9462a8 --- /dev/null +++ b/devdocs/c/types%2Fnull.html @@ -0,0 +1,47 @@ +

NULL

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 macro NULL is an implementation-defined null pointer constant, which may be

+ + +
(since C23)

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

+

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

Example

#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);
+}

Possible output:

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

See also

+ +
+
(C23)
the type of the predefined null pointer constant nullptr
(typedef)
C++ documentation for NULL
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/types/NULL +

+
-- cgit v1.2.3