summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Fnullptr.html
blob: 9c5d8a87cc51d8d7c55ce053d8a47a98c441819e (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
48
49
50
51
52
53
54
55
56
57
58
59
    <h1 id="firstHeading" class="firstHeading">Predefined null pointer constant <span class="t-mark-rev t-since-c23">(since C23)</span>
</h1>             <h3 id="Syntax"> Syntax</h3> <table class="t-sdsc-begin">  <tr class="t-sdsc"> <td> <code>nullptr</code> </td> <td class="t-sdsc-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td>
</tr> 
</table> <h3 id="Explanation"> Explanation</h3> <p>The keyword <code>nullptr</code> denotes a predefined null pointer constant. It is a <a href="value_category#Non-lvalue_object_expressions" title="c/language/value category">non-lvalue</a> of type <code><a href="../types/nullptr_t" title="c/types/nullptr t">nullptr_t</a></code>. <code>nullptr</code> can be <a href="conversion" title="c/language/conversion">converted</a> to a pointer types or <code>bool</code>, where the result is the null pointer value of that type or <code>false</code> respectively.</p>
<h3 id="Keywords"> Keywords</h3> <p><a href="../keyword/nullptr" title="c/keyword/nullptr"><code>nullptr</code></a></p>
<h3 id="Example"> Example</h3> <div class="t-example">
<p>Demonstrates that a copy of <code>nullptr</code> can also be used as a null pointer constant.</p>
<div class="c source-c"><pre data-language="c">#include &lt;stddef.h&gt;
#include &lt;stdio.h&gt;
 
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
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Function g called
Function g called
Function g called
Function g called
void*
integer
nullptr_t
void*</pre></div> </div> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../types/null" title="c/types/NULL"> <span class="t-lines"><span>NULL</span></span></a></div> </td> <td> implementation-defined null pointer constant <br> <span class="t-mark">(macro constant)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../types/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 <strong class="selflink"><code>nullptr</code></strong> <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/language/nullptr" title="cpp/language/nullptr">C++ documentation</a></span> for <code>nullptr</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/language/nullptr" class="_attribution-link">https://en.cppreference.com/w/c/language/nullptr</a>
  </p>
</div>