[c++] nullptr
Before the introduction of nullptr
, the NULL
macro or plain 0
was commonly used to represent a null pointer. However, using nullptr
is preferred because it has a distinct type, std::nullptr_t
, and can be used in contexts where the old-style NULL
or 0
might lead to ambiguities or errors.
Here’s an example of using nullptr
in C++:
int* ptr = nullptr; // initializing a pointer with nullptr
In this example, ptr
is assigned a null pointer using nullptr
.
Using nullptr
instead of NULL
or 0
can improve code readability and help catch certain types of errors, making it a recommended practice in modern C++ programming.
Reference:
- Stroustrup, Bjarne. (2013). The C++ Programming Language, Fourth Edition. Addison-Wesley Professional.