Concepts / Pointers in C++

Pointers in C++

nullptr is the null pointer keyword in C++ and is reserved, so it cannot be used as a variable name.

  • Programming

Why a Pointer Needs a Clear Empty State

A pointer is a variable that is meant to refer to something else. Sometimes a pointer has nothing valid to refer to yet, or you want to record that it currently refers to nothing. C++ needs a clear way to write that state directly in code. The keyword nullptr fills this role. It is the null pointer keyword, and this article explains what it is and how it changes the state of a pointer.

What do you think happens?

When a pointer is set to nullptr, what does it hold, and how is that different from a pointer that points at a real variable?

Reveal answer
Answer:

A pointer set to nullptr is in the null pointer state: it is explicitly holding the null pointer value rather than pointing at a variable.

A pointer that points at a real variable refers to an object. A null pointer refers to nothing. Keeping these two cases distinct is the main idea of the rest of this article.

Hold that question in mind as you read. The sections below answer it step by step by following a pointer through the states it can be in.

Following a Pointer Through Its States

Think of a pointer's life as a short sequence of states. It starts out as a named pointer variable with no target chosen. An assignment of nullptr then moves it into the null pointer state, where it is explicitly holding the null pointer value rather than pointing at a variable.

Declared pointernamed variable, no targetchosenAssignment of nullptrexplicit assignment in codeNull pointer stateholds the null pointervalueassign nullptrpointer now null
Before and after the assignment of nullptr, which state is the pointer in? The change happens at the assignment step.

Reading the diagram from left to right, the important change happens at the assignment step. Before that step the pointer is only declared. After it, the pointer is in the null pointer state, and that state is what nullptr represents.

What nullptr Represents

Definition

nullptr is the null pointer keyword in C++. It is reserved, so it cannot be used as a variable name. Its job is to write the null pointer state explicitly in code.

The word keyword matters here. A keyword is a name that the language has already claimed for its own use, so you cannot reuse it for your own variables, functions, or other identifiers. nullptr belongs to that group, which is why it is a fixed part of the language rather than a name you choose.

Pointer prefers to a real variableVariable xobject the pointer referstoPointer qnull pointer staterefers to
How does a pointer that refers to a real variable differ from a pointer in the null pointer state?

In the diagram, pointer p has a real variable to refer to, while pointer q is in the null pointer state and has no variable to point at. The difference lies in what the pointer holds, not in the variable's own value.

Declaring Versus Assigning nullptr

A common point of confusion is whether declaring a pointer is enough to put it in the null pointer state. It is not. Declaring a pointer alone does not place it in the null pointer state; an explicit assignment does. The most common way to write that assignment is initialization, which gives the pointer the null pointer value at the moment it is declared.

cpp
Notes on the code
  • int* declares a pointer that is meant to refer to an int
  • p is the name of the pointer variable
  • = nullptr gives the pointer the null pointer value from its first line
Before
int* p;

Declared only. The pointer has no explicit null pointer state.

After
int* p = nullptr;

Explicitly in the null pointer state from the first line of the program.

Initializing with nullptr makes the state visible from the first line, so a reader does not have to search later in the program to learn what the pointer holds.

Tracing an Assignment Step by Step

Worked example

Declaring a pointer and then assigning nullptr

Trace the state of pointer p through a two-statement sequence: first a declaration with no initializer, then an explicit assignment of nullptr.

  1. 1
    Step 1: declare the pointer

    The statement int* p; creates a named pointer variable. Per the source, this alone does not place p in the null pointer state, so p has no explicit null pointer state yet.

  2. 2
    Step 2: assign nullptr

    The statement p = nullptr; is an explicit assignment. This is the step that moves p into the null pointer state.

  3. 3
    Step 3: read the state

    After the assignment, p is in the null pointer state and holds the null pointer value, not a reference to a variable.

Result

The change of state happens at the assignment, not at the declaration.

cpp
Notes on the code
  • Line 1 declares p without setting an explicit null state
  • Line 2 is the explicit assignment that places p in the null pointer state

Mistakes Beginners Make with nullptr

  • Common mistake

    Using nullptr as a variable name

    What it looks like
    int nullptr = 5;
    Why it fails

    nullptr is a reserved keyword, so the language does not allow it to be used as an ordinary identifier.

    Do this instead

    Choose a different name for the variable, such as count or value, and keep nullptr for the null pointer value.

  • Common mistake

    Assuming a bare declaration already sets the null pointer state

    What it looks like
    int* p; // assumed to be null
    Why it fails

    Declaring a pointer alone does not place it in the null pointer state; an explicit assignment does.

    Do this instead

    Write int* p = nullptr; so the null state is explicit from the first line.

  • Common mistake

    Treating a null pointer as if it refers to a real variable

    Why it fails

    A pointer in the null pointer state holds the null pointer value and is not pointing at a variable, so there is no object for it to refer to.

    Do this instead

    Track which state each pointer is in, and keep the null state distinct from a pointer that refers to a real variable.

Practicing State Tracking

EASY

Consider the sequence int* p; then int* q = nullptr; then p = nullptr;. After each statement, state whether p and q are in the null pointer state. Which statement is the one that moves p into the null pointer state, and why does the declaration of q not need a separate assignment?

Hints
  • Check which statement is a declaration alone and which contains an explicit assignment of nullptr.
  • The source says that declaring a pointer alone does not place it in the null pointer state, but an explicit assignment does.
  • Initialization with = nullptr counts as the explicit assignment in the declaration itself.

Key Takeaways

  • nullptr is the null pointer keyword in C++, and it is reserved, so it cannot be used as a variable name.
  • A pointer moves into the null pointer state through an explicit assignment of nullptr; declaring the pointer alone does not do this.
  • Initializing with int* p = nullptr; makes the pointer's state visible from the first line of the program.
  • A pointer in the null pointer state holds the null pointer value and is distinct from a pointer that refers to a real variable.

Related concepts

More concepts starting with P