diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/types%2Fnull.html | |
new repository
Diffstat (limited to 'devdocs/c/types%2Fnull.html')
| -rw-r--r-- | devdocs/c/types%2Fnull.html | 47 |
1 files changed, 47 insertions, 0 deletions
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 @@ + <h1 id="firstHeading" class="firstHeading">NULL</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><locale.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><stddef.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><string.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><time.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dsc-header"> <th> Defined in header <code><wchar.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define NULL /*implementation-defined*/</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>The macro <code>NULL</code> is an implementation-defined null pointer constant, which may be</p> +<ul> +<li> an integer <a href="../language/constant_expression#Integer_constant_expression" title="c/language/constant expression">constant expression</a> with the value <code>0</code> </li> +<li> an integer constant expression with the value <code>0</code> <a href="../language/conversion#Pointer_conversions" title="c/language/conversion">cast to the type</a> <code>void*</code> </li> +</ul> <table class="t-rev-begin"> <tr class="t-rev t-since-c23"> +<td> <ul><li> predefined constant <a href="../language/nullptr" title="c/language/nullptr"><code>nullptr</code></a> </li></ul> </td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td> +</tr> </table> <p>A null pointer constant may be <a href="../language/conversion#Pointer_conversions" title="c/language/conversion">converted</a> to any pointer type; such conversion results in the null pointer value of that type.</p> +<h3 id="Possible_implementation"> Possible implementation</h3> <table class="eq-fun-cpp-table"> <tr> <td> <div class="c source-c"><pre data-language="c">// 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</pre></div> </td> +</tr> +</table> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#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); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">(nil) (nil) (nil) +ptr = 0xc001cafe</pre></div> </div> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="nullptr_t" title="c/types/nullptr t"> <span class="t-lines"><span>nullptr_t</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div> </td> <td> the type of the predefined null pointer constant <a href="../language/nullptr" title="c/language/nullptr"><code>nullptr</code></a> <br> <span class="t-mark">(typedef)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/types/NULL" title="cpp/types/NULL">C++ documentation</a></span> for <code>NULL</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/types/NULL" class="_attribution-link">https://en.cppreference.com/w/c/types/NULL</a> + </p> +</div> |
