C++ is an amazing programming language. Variables and memory usage are important in programming, and how we handle these are improved in every C++ standard. One of the great features is null pointer constants in modern C++ which was introduced with the C++11 standard. In this post, we explain what a null pointer constant (nullptr ) is with very simple examples. You can learn C++ and follow along with our examples with any professional IDE and compiler that supports C++11, C++14, C++17 or above CLANG standards.
What is a pointer in modern C++?
Pointers are variables that hold addresses. The asterisk character ‘*
’ is used to define pointers. It is used before the variable name. Pointers are some of the strongest aspects of the C & C++ programming languages. They allow you to reach any kind of type (including very large-sized bitmaps or videos or other data) without copying the whole data which means they can be very fast to use. If you still don’t know how to use pointers, we explain them in the following article.
What is a Null pointer constant (nullptr) in modern C++?
The value (contents) of a pointer type is normally an address value, a positive integer number that indicates where in the computer’s memory some data can be found. If there is no address to point to (no pointer value), you can set this value to NULL. In some cases, 0 is used. In C++, some examples show that these are not enough in some scenarios so there needs a new pointer constant that can be used for features of modern C++ like templates, or as a parameter of a function.
The nullptr keyword is a null pointer constant which is a prvalue of type std::nullptr_t
. that denotes the pointer literal. C++11 introduced nullptr
, the null pointer constant, to remove the ambiguity between 0 and a null pointer. Although the old style NULL macro exists, it is insufficient because it cannot be distinguished from the integer 0 in a call to a function that has one overload with an int parameter and another with a char* parameter.
Therefore, nullptr
is now a reserved word. The integer 0 will not be implicitly converted to any pointer type. The null pointer can only be converted to any pointer type. The null pointer cannot be used in an arithmetic expression, assigned to an integral value, or compared to an integral value. If you want to know more details about nullptr
, it is explained well in the Null pointer constant Proposal document.
Here is the syntax for nullptr
.
Syntax:
1 |
nullptr |
here is an example how you can use this constant.
1 |
int *f = nullptr; |
It can be used as a null pointer constant parameter of a function as below.
1 |
my_func(nullptr); |
Is there an example of how to use the nullptr null pointer constant in modern C++?
Here is an example of nullptr
, the null pointer constant in modern C++ that is used as a function parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> void my_func(int *f) { // } main() { my_func(0); // OK my_func(NULL); // OK my_func(nullptr); // OK } |
In the example above we may use NULL
or 0
as a parameter too. The example below shows that you cannot use NULL
or 0
in template usage – the nullptr
is the only solution here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> template<class T> constexpr T my_temp(const T& x) { return x; } void my_func(int *f) { // } int main() { my_func(my_temp(nullptr)); // OK // my_func(my_temp(0)); // ERROR // my_func(my_temp(NULL)); // ERROR } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.