Concepts / Pointers and Initialization

Pointers and Initialization

nullptr is the explicit null pointer value, so its spelling states the intent directly. Use it both when you initialize a pointer and when you test whether a pointer refers to an object.

  • Programming

Why a Pointer Sometimes Points at Nothing

What do you think happens?

You need a pointer that does not yet refer to any object. Which of these spellings would you write: nullptr, 0, or NULL?

  • nullptr

  • 0

  • NULL

Reveal answer
Answer:

nullptr

The source recommends nullptr rather than 0 or NULL for the null pointer value. The rest of this article explains why.

Sometimes a pointer has to exist before there is anything for it to point at. A program might declare a pointer now and assign its target later. Or it might need a way to say that a pointer currently refers to no object at all. C++ needs a value for that situation, and the value you write in your source code matters.

Several different spellings have been used for this value over the years, so a learner will quickly run into three of them: nullptr, 0, and NULL. The question this article answers is which spelling you should use when you need to write the null pointer value.

The Recommendation: Write What You Mean

The source gives one short rule: use nullptr rather than 0 or NULL. The rule is short, but it carries a clear intent. When you write nullptr, the code states that a pointer is being set to the null pointer value. The spelling itself says what is meant.

Compare this with writing 0 in the same position. The reader has to remember that 0 is being used as a pointer value and not as a plain number. NULL asks the same kind of memory work from the reader. The recommendation asks you to choose the spelling that removes that work.

Key point

nullptr is the explicit null pointer value, so its spelling states the intent directly. Use it both when you initialize a pointer and when you test whether a pointer refers to an object.

Three Spellings Compared

All three spellings appear in older code and in discussions of pointers, which is why the recommendation exists. The table below summarizes the role each spelling plays. The source gives a single preference, so the table focuses on role rather than on details the source does not cover.

SpellingRole in pointer codeStatus in the recommendation
nullptrThe explicit null pointer value; its spelling states that a pointer is being set to nullRecommended for initializing and testing pointers
0A plain number that is written in a pointer position; the reader must remember it is a pointer valueNot recommended
NULLAnother spelling of the null pointer value seen in older code and discussionsNot recommended

The three candidate values a beginner will meet for a null pointer

nullptrexplicit null pointer value0plain number in pointerpositionNULLolder spelling of the nullvalue
Which spelling should you write for the null pointer value? Only nullptr is recommended; 0 and NULL are older candidates that the reader has to interpret.

Null Pointer Versus Pointer to an Object

A pointer initialized to nullptr refers to no object. A pointer that holds the address of a variable refers to that variable. The diagram below places the two side by side, using one variable x as the object that the second pointer refers to.

pnullptr: refers to noobjectqholds the address of xx5refers to
What does a pointer initialized to nullptr hold, compared with a pointer that holds the address of a variable? The first pointer holds the null value, while the second refers to x.

Because p holds the null value, a program that wants to use p as a pointer to an object must first find out whether p refers to anything. That check is the subject of the next section.

Declaring and Checking a Null Pointer

The clearest way to see the recommendation in use is to declare a pointer and give it the null value at the moment it is created. The example below is written for teaching purposes. It declares an int pointer, initializes it to nullptr, and checks it before doing anything with it.

Code walkthrough
#include <iostream>

int main() {
    int* p = nullptr;

    if (p != nullptr) {
        std::cout << *p << "\n";
    } else {
        std::cout << "p does not refer to an object\n";
    }
    return 0;
}
  1. 1
    Line 4 declares p as a pointer to int and initializes it to nullptr, so p refers to no object.
  2. 2
    Line 6 compares p with nullptr. Because the comparison is written against nullptr, it reads as plain English.
  3. 3
    Line 7 dereferences p only inside the branch where the check says p refers to an object.
  4. 4
    Line 9 runs for this program, because the check found that p is nullptr, and prints a message instead of touching the pointer.
  • int* p declares a pointer to int
  • = nullptr gives the pointer the explicit null value at creation
  • p != nullptr is the check made before the pointer is used
  • The else branch runs because p was initialized to nullptr
Output
p does not refer to an object

Notice that the comparison reads as plain English: the pointer is compared to nullptr. A learner reading this code does not have to decide whether a 0 in that position is a number or a pointer. The spelling itself carries the meaning.

Checking Before Use

The order of steps matters. A pointer is checked first, and only the path that matches the check is taken. When the pointer is nullptr, the program does not use it as a pointer to an object. When the pointer holds the address of a valid object, the program may use it.

Declare pointer pinitialized to nullptr orto an addressIs p != nullptr?check before useUse the object prefers tovalid address pathSkip the usenullptr pathyesno
Is the pointer checked first, and which path is taken when it is nullptr versus when it holds a valid address? The check comes first, and the two outcomes split the path.

Mistakes Beginners Make with Null Values

  • Common mistake

    Writing 0 in a pointer position because it looks like a valid starting value

    What it looks like
    int* p = 0;
    Why it fails

    The reader has to remember that 0 is being used as a pointer value and not as a plain number. The recommendation exists to avoid exactly that interpretation step.

    Do this instead
    int* p = nullptr;
  • Common mistake

    Mixing 0, NULL, and nullptr in the same codebase

    Why it fails

    Readers then have to decide which spelling is meant at each place. A codebase that uses one spelling asks for one decision only.

    Do this instead

    Pick nullptr and use it for every null pointer value in the code.

  • Common mistake

    Using a pointer without checking whether it refers to an object

    What it looks like
    int* p = nullptr;
    std::cout << *p;
    Why it fails

    A pointer initialized to nullptr refers to no object, so the program has no object to use. The source's guidance is to check pointers against nullptr when testing whether they refer to an object.

    Do this instead

    Compare p with nullptr first, and use *p only in the branch where p refers to an object.

Practice: Choose and Check

EASY

Write a declaration of an int pointer named ptr that is initialized to the null pointer value using the recommended spelling. Then write an if statement that checks whether ptr refers to an object, using the same spelling in the comparison.

Hints
  • Use the spelling the source recommends, not 0 or NULL.
  • The check compares ptr with the same null spelling you used to initialize it.
  • Put any use of the pointer inside the branch that runs when ptr refers to an object.

Key Takeaways

  • The source recommends nullptr rather than 0 or NULL for the null pointer value.
  • The three spellings a beginner will meet are nullptr, 0, and NULL; only nullptr states the intent directly.
  • Initialize pointers with nullptr and compare them against nullptr before using them to refer to an object.
  • Using one explicit null value in every place keeps pointer code easy to read, because the reader never has to decide which meaning was intended.

Related concepts

More concepts starting with P