Arrays, Pointers, and Dereferencing
A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.
Testing a Familiar Belief
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 seem to 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?
Before reading further, predict what a compiler does with this line: int n = nullptr; If nullptr were a zero-like integer, this would be an ordinary declaration. What happens instead?
Reveal answer
The compiler rejects the line with a compile error, because nullptr is a pointer value and cannot be assigned to an int.
nullptr is a pointer value, not an integer, so assigning it to an int is an error. The rest of this article explains why.
What a Null Pointer Holds
A null pointer is a pointer whose value is nullptr and which does not refer to a valid object.
The important word is value. A null pointer is still a perfectly good variable with a value. That value simply says that no object is being referred to. A valid pointer, by contrast, holds a value that refers to an object, and dereferencing it reaches that object. The diagram below separates the two cases: one pointer holds nullptr and refers to nothing, while another refers to an ordinary object.
The diagram is deliberately simple. Pointer p has no edge leading to an object, because a null pointer refers to no object. Pointer q has an edge to an object, because it refers to one. The diagram makes no claim about where in memory these items are located, and neither does this article.
Why Programs Assign nullptr
The reason programs use this value comes from a goal: 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.
Imagine a linked list where each node holds a pointer to the next node. The last node has no next node to point to. Giving its next pointer the value nullptr records that there is nothing further in the list, so a traversal code can stop when it sees nullptr instead of following a pointer to nothing.
One nullptr for Every Pointer Type
There is only one nullptr, and it is shared by all pointer types. A pointer to an int and a pointer to a double both use that same null value. The type of a pointer still matters for what it could point to, but the null value itself is common to every pointer type.
Why nullptr Is Not an Integer
The zero intuition breaks at this point. nullptr is a pointer value, not an integer. Zero is an int value. Because the two belong to different families, they are not interchangeable. Assigning nullptr to an int is an error, and a compiler will reject a line like the one below.
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 answer before relying on it. The source recommends this habit directly: it is often wise to check that a pointer which is supposed to point to something actually points to something. Validating a pointer means testing it against nullptr before dereferencing it, so that code acts on a pointer only when it refers to an object.
The check is simple. The pointer is compared with nullptr, and the code path depends on the result. If the comparison says the pointer is nullptr, the dereference is skipped. If the pointer refers to an object, the dereference runs. The flowchart lays out the two branches so you can follow the control flow for each case.
5- p is set to nullptr, so it refers to no object
- q is set to the address of value, so it refers to an object
- The dereference *p is guarded and never runs
- The dereference *q runs and reads the object's value
Tracing the guarded dereference
A program has an int pointer p set to nullptr and an int pointer q that refers to an int holding 5. Which dereferences produce output?
- 1Check p
p holds nullptr, so the comparison p != nullptr is false and the guarded block is skipped.
- 2Check q
q refers to an object, so the comparison q != nullptr is true and the guarded block runs.
- 3Dereference q
The dereference *q follows q to the object and reads the value 5.
Only the dereference through q produces output, and the printed value is 5.
Mistakes Learners Make with nullptr
Common mistake Treating nullptr as a special kind of zero
What it looks likeint n = nullptr;Why it failsnullptr is a pointer value, not an integer, so assigning it to an int is an error.
Do this insteadUse an int value such as 0 for an integer, and keep nullptr for pointers.
Common mistake Dereferencing a pointer without checking it first
What it looks likeint *p = nullptr; int x = *p;Why it failsThe source says the pointer should be compared with nullptr before dereferencing, so the dereference runs only when the pointer refers to an object. Here p refers to no object.
Do this insteadCompare the pointer with nullptr first and dereference only inside the branch where it refers to an object.
Common mistake Assuming each pointer type has its own null value
Why it failsThere is only one nullptr, shared by all pointer types.
Do this insteadAssign nullptr to any pointer type, and remember that the type still controls what the pointer could refer to.
Practice Checking Pointers
A function receives an int pointer p. Write the guard that makes the dereference *p happen only when p refers to an object. Then state what happens if p is nullptr.
Hints
- The source says to compare the pointer with nullptr before dereferencing it.
- Put the dereference inside the branch that runs when the comparison shows the pointer is not nullptr.
Explain, without running code, why the line double d = nullptr; is accepted by a compiler while int n = nullptr; is rejected. Use the idea that nullptr is a pointer value, not an integer.
Hints
- Think about what kind of value nullptr is, not about what its spelling suggests.
- A double variable and an int variable both hold numbers, but neither is a pointer.
Key Takeaways
- A null pointer is a pointer whose value is nullptr, and it does not refer to a valid object.
- Programs give a pointer the value nullptr when no object exists yet or when they need to express that no object is available, such as the end of a linked list.
- 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.
- Compare a pointer with nullptr before dereferencing it, so the dereference runs only when the pointer refers to an object.