C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn What A Null Pointer Constant (nullptr) Is In Modern C++

Learn What A Null Pointer Constant nullptr Is In Modern C++

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:

here is an example how you can use this constant.

It can be used as a null pointer constant parameter of a function as below.

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.

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.

Learn What A Null Pointer Constant nullptr Is In Modern C++ The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?