Pointers and References
A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.
Testing the Belief That nullptr Is Zero
Many learners meet nullptr and quietly file it away as a special kind of zero. The reasoning feels natural: nullptr is a word that means nothing is there, zero is the number that means nothing is there, so the two must be the same kind of thing. This article tests that belief. The short version is that nullptr belongs to the family of pointer values, not the family of integers, and that difference decides whether a line of code compiles at all.
What do you think happens?
If nullptr were a zero-like integer, the line below would be a normal declaration. What does the compiler do with it: int x = nullptr;
Reveal answer
It reports a compile error.
nullptr is a pointer value, not an integer. Assigning it to an int is an error, so the belief that nullptr is a zero-like integer does not hold.
Following a Pointer from Object to nullptr
A pointer's value is what decides what it refers to. Start with a pointer that refers to an ordinary object. Later, the program assigns the pointer the value nullptr. At that moment the pointer still exists and still holds a value, but the value now says that no object is being referred to. Follow the two steps in the diagram below and notice what state the pointer is in after each one.
The pointer is still a perfectly good variable with a value. Only the meaning of that value has changed: it now says that no object is present. A pointer in this state is called a null pointer.
What a Null Pointer Is For
A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.
The goal behind pointers is that a pointer should always point to an object, so that using it through a dereference is valid. Real programs cannot always meet that goal. Sometimes there is no object to point to yet. Sometimes the program needs a way to express that no object is available, such as marking the end of a linked list. For those situations the pointer is given the value nullptr.
Pointers of different types still share the same null value. The source states that there is only one nullptr, shared by all pointer types. A pointer to a list link and a double pointer both use that same null value. The type of a pointer still determines what it could point to, but the null value itself is common to every pointer type.
The diagram is deliberately simple. It shows the relationship that matters here: a null pointer has no object on its far side, while a valid pointer does. It makes no claim about where in memory either item lives, because the source does not describe that.
nullptr Is a Pointer Value, Not an Integer
The original belief fails at exactly this point. nullptr is a pointer value, not an integer, so it cannot be assigned to an int. The two declarations below have the same shape, but only the second one is valid, because it declares a pointer whose value is nullptr.
- The first line fails to compile because nullptr cannot be stored in an int.
- The second line is valid because the variable is a pointer, and nullptr is a pointer value.
| Property | Zero as an integer | nullptr |
|---|---|---|
| Family of values | Integers | Pointer values |
| Can be assigned to an int | Yes | No, this is a compile error |
| Can be assigned to a pointer | No | Yes |
| Meaning | The number zero | A pointer that refers to no valid object |
Why nullptr and the integer zero are not interchangeable
Checking a Pointer Before Dereferencing
A null pointer is useful because it lets a program say that no object is present. That only helps if the program looks at the value before relying on it. The source recommends this habit directly: it is often wise to check that a pointer argument that is supposed to point to something actually points to something. Validating a pointer means testing it against nullptr before the dereference, so that code acts on the pointer only when it refers to an object.
- The comparison p != nullptr decides whether the dereference is reached.
- When p holds nullptr, the body of the first if is skipped, so the dereference never happens.
- When q refers to an object, the same check lets the dereference run.
Deciding whether a dereference runs
A function receives a pointer p. Show which branch runs when p holds nullptr and when p refers to an object.
- 1Compare the pointer
Evaluate p != nullptr before any dereference is attempted.
- 2Case one: p holds nullptr
The comparison is false, so the dereference is skipped and the program continues without using p.
- 3Case two: p refers to an object
The comparison is true, so the dereference runs and the object is used through p.
The dereference runs only when p refers to an object, which is exactly the protection the check is meant to provide.
Mistakes Beginners Make with nullptr
Common mistake Treating nullptr as the integer zero
What it looks likeint count = nullptr;Why it failsnullptr is a pointer value, and assigning it to an int is an error.
Do this insteadStore a null value only in a pointer, such as int* p = nullptr;Common mistake Dereferencing a pointer without checking it first
What it looks likeint* p = nullptr; *p = 20;Why it failsA null pointer does not refer to a valid object, so using it through a dereference is not valid.
Do this insteadCompare with nullptr first, and dereference only inside the branch where the pointer refers to an object.
Common mistake Assuming all pointer types need different null values
Why it failsThe source states that there is only one nullptr, shared by all pointer types.
Do this insteadUse nullptr for any pointer type; the type of the pointer still controls what it could point to.
Predicting the Branch for a Null Pointer
Consider the code below. Before running it or reading further, decide whether the line marked as a dereference executes, and explain why. Then decide whether the program would be valid if the first line were changed to int* p = nullptr; with no check at all.
int value = 5;
int* p = nullptr;
if (p != nullptr) {
*p = 7;
}Hints
- Ask what value p holds on the first line of the if statement.
- Ask whether the comparison with nullptr is true or false for that value.
- Think about the difference between a null pointer and a pointer that refers to an object.
Key Takeaways
- A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.
- nullptr is a pointer value, not an integer, so assigning it to an int is a compile error.
- There is one nullptr shared by all pointer types, and it is used when no object is available, such as marking the end of a linked list.
- Compare a pointer with nullptr before dereferencing it, so the dereference runs only when the pointer refers to an object.