diff options
Diffstat (limited to 'devdocs/c/language%2Fnullptr.html')
| -rw-r--r-- | devdocs/c/language%2Fnullptr.html | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fnullptr.html b/devdocs/c/language%2Fnullptr.html new file mode 100644 index 00000000..9c5d8a87 --- /dev/null +++ b/devdocs/c/language%2Fnullptr.html @@ -0,0 +1,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 <stddef.h> +#include <stdio.h> + +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"> + © 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> |
