blob: fc9462a8dea29d5267a7355327e70a3fe970b973 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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>
|