C++ Pointers and References: Addresses, Dereferencing, and nullptr
The same guide shows the one difference that causes the most confusion: a pointer can be re-pointed, a reference cannot, and assigning to a reference writes through it instead.
Written November 11, 2026 · Tested with GCC 13.3.0 · 11 min read
- What an address is
- Pointer versus reference: reseating
- Pass by value versus pass by reference
- Pointer arithmetic and why (q + 2) is arr[2]
What this guide is
A pointer is an object that holds the address of another object. A reference is an alias: a second name for an object that already exists. The program uses GCC 13.3.0 with -std=c++17 -Wall -Wextra and exits with code 0.
The program
#include <iostream>
void add_one(int& n) { n += 1; }
int main() {
int value = 10;
int* p = &value;
int& ref = value;
*p = 20;
std::cout << "value=" << value << " *p=" << *p << " ref=" << ref << "\n";
add_one(ref);
std::cout << "after add_one value=" << value << "\n";
int* nullp = nullptr;
std::cout << "nullp is null: " << (nullp == nullptr) << "\n";
int arr[3] = {1, 2, 3};
int* q = arr;
std::cout << "arr[0]=" << *q << " arr[2]=" << *(q + 2) << "\n";
return 0;
}
The exact output:
value=20 *p=20 ref=20
after add_one value=21
nullp is null: 1
arr[0]=1 arr[2]=3
Three details in the output deserve a second look before the sections below explain them. First, value, *p, and ref all print 20 after a single write through the pointer: the pointer and the reference are two ways of reaching the same storage, not two copies of it. Second, add_one changes value even though the call passes ref, because the parameter is a reference and the function writes to the caller's object directly. Third, nullp is null prints 1, not true: a comparison yields a bool, and std::cout prints a bool as a number by default, the same rule the variables guide shows for a plain bool.
What an address is
Every object in a running program lives at an address: a location in memory where its bytes sit. The address-of operator & returns that location, and the dereference operator * follows a pointer back to the object at its address. In the program, int* p = &value; stores the address of value in p, and *p = 20; writes 20 into the object at that address, which is value itself. The declaration reads as a pair: int* says what the pointer points to, and &value says where that object is.
The reference implementation of the language states the same rule in one sentence: a pointer that points to an object represents the address of the first byte in memory occupied by the object. That sentence is the whole mental model. A pointer is not the value it points to, and it is not a copy of that value. It is the address of the first byte, and following it reaches the object.
Two spellings in the declaration deserve attention because both are legal and they confuse readers in opposite directions. int* p puts the star next to the type, and int *p puts it next to the name. The compiler reads both the same way, but the second spelling is honest about a quirk: in int* a, b; only a is a pointer, while b is a plain int, because the star binds to the declarator, not to the type. Declare one pointer per statement and the quirk never bites.
Pointer versus reference: reseating
The one difference between a pointer and a reference that matters most is reseating: whether the name can be made to point at a different object later. A pointer can. A reference cannot. The reference is bound once, at initialization, and stays bound to that object for its whole lifetime. Assigning to a reference does not rebind it. It writes through it to the object it already names.
The box run proves it with the smallest possible case:
#include <iostream>
int main() {
int a = 1, b = 2;
int* p = &a;
p = &b;
int& r = a;
r = b;
std::cout << "p points at b: " << (*p == 2) << " a=" << a << " r=" << r << "\n";
return 0;
}
The exact output:
p points at b: 1 a=2 r=2
Read the output line by line. p = &b; re-points the pointer, so *p is now 2 and the check prints 1. r = b; does not rebind the reference. It copies the value of b into a, so a is 2 and r, which still names a, prints 2. If r had been re-pointed to b, then a would still be 1. It is not, and that is the proof.
A reference must also be initialized when it is declared: the language requires a reference to be initialized to refer to a valid object or function. There is no such thing as a declared-but-unbound reference waiting for a later assignment, because an assignment to a reference is a write to its object, not a binding. A pointer has no such rule. It can be declared without an initializer, which is exactly how an uninitialized pointer is born, and the trap for that case is in the traps section below.
The practical rule follows from the difference. Use a reference when the name must always refer to one object and never be empty: a function parameter that the function will modify, or an alias that shortens a long expression. Use a pointer when the name may be empty, may be re-pointed, or must be able to say "nothing yet". nullptr is the pointer spelling of "nothing yet", and references have no equivalent, because a reference is always bound to something.
Pass by value versus pass by reference
A function parameter declared as a plain value receives a copy. A parameter declared as a reference receives the caller's object itself. The program shows the second case: add_one takes int& n, the call passes ref, and the += 1 inside the function changes value in main from 20 to 21. The output line after add_one value=21 is the proof. Nothing is returned, nothing is copied back. The write happens in place.
The copy case is worth stating because the difference is the whole point of the choice. If add_one had taken int n instead, the call would have copied 20 into a local variable, added one to the copy, and thrown the copy away when the function returned. value would still be 20, and the program would print 20 instead of 21. The signature is the contract: int n says the function works on its own copy, int& n says it works on the caller's object, and const int& n says it reads the caller's object without changing it. The last form is the common one for large objects, where a copy would be wasteful and a plain reference would promise a write the function does not make.
Pass by reference also explains a common error message. A reference parameter cannot bind to a temporary value that has no address to take, so a call that passes a literal where a non-const reference is expected fails to compile. The compiler is protecting the same invariant: a reference names a real object, and a literal is not one.
Pointer arithmetic and why *(q + 2) is arr[2]
An array name converts to a pointer to its first element, so int* q = arr; makes q point at arr[0]. Adding an integer to a pointer moves it by that many elements, not by that many bytes: q + 2 points at the third element, because each step advances by the size of one int. Dereferencing that pointer, *(q + 2), reads the third element, which is 3. The program output confirms it: arr[0]=1 arr[2]=3.
The rule is the same for every pointer to an array element. q + 1 is the address of arr[1], q - 1 would be the address before the array, and q + 3 is one past the end: a valid pointer value that must not be dereferenced, because there is no element there. Pointer arithmetic is defined only within an array, including the one-past-the-end position. Moving a pointer outside that range, or dereferencing the one-past-the-end pointer, is undefined behavior.
The same arithmetic is what makes a pointer a usable iterator over an array. A loop that starts at arr and advances while the pointer is not equal to arr + 3 visits exactly the three elements, and the comparison works because pointers to elements of the same array compare in the same order as their indices. That is also why the standard library can treat a raw array as a range: the pointer arithmetic and the comparison rules are the same ones its algorithms rely on.
nullptr versus NULL versus 0
A pointer that points at nothing holds the null pointer value, and C++ spells it nullptr. The program declares int* nullp = nullptr; and the comparison nullp == nullptr prints 1, confirming the pointer holds the null value. nullptr is a keyword with its own type, std::nullptr_t, and it converts to the null pointer value of any pointer type. That is the whole reason to use it: it can only ever mean "no object".
The two older spellings still compile, and both are worse. NULL is a macro, an implementation-defined null pointer constant, and 0 is an integer literal that also converts to a null pointer. Both can surprise overload resolution: a call that passes 0 where a function takes either an int or a pointer may pick the int overload, while nullptr picks the pointer overload, because nullptr is not an integer. The reference documentation shows the same trap in miniature: a function taking int* accepts nullptr, NULL, and 0 alike, but a value passed through a template loses the null meaning for NULL and 0, while nullptr keeps it.
The rule is short. New code writes nullptr and nothing else. NULL and 0 appear only in old code and in interfaces that demand them, and a reader who sees 0 assigned to a pointer should read it as a null pointer written before nullptr existed.
Trap 1: dereferencing a null pointer
* to follow. Dereferencing one is undefined behavior: the program may crash, may appear to work, or may do anything else, and the compiler owes no diagnostic for it. This guide does not run the crashing program, because the crash is not the lesson. The lesson is the check that prevents it: test the pointer before following it.if (p != nullptr) {
std::cout << *p << "\n";
}
The check costs one comparison and removes the whole class of failure. The pattern belongs anywhere a pointer can legitimately be empty: a function that returns nullptr on failure, a pointer that starts empty and is filled in later, or an optional output parameter. A pointer that can never be null should be a reference instead, so the question never arises.
Trap 2: a pointer that outlives its object
int* p = nullptr;
{
int local = 42;
p = &local;
} // local goes out of scope here; p now dangles
// *p is undefined behavior from this line on
The fix is a lifetime rule, not a check: a pointer must never outlive the object it points to. Return a value or a smart pointer from a function instead of the address of a local, keep the pointed-to object alive for as long as any pointer to it exists, and prefer a reference for a short-lived alias inside one scope, where the compiler can see that both names end together.
Trap 3: an uninitialized pointer
int holds an indeterminate number. Reading that value is undefined behavior, and following the pointer reads or writes memory at an address the program never chose. The compiler can warn about the easy cases, as the variables guide shows for an uninitialized int, but it stays silent in the harder ones, where the use sits behind a branch or a function call.The fix is the same in both cases. Initialize at the point of declaration, and prefer nullptr when there is no meaningful address yet, so the pointer is empty rather than indeterminate:
int* p = nullptr; // empty on purpose, checked before use
An initialized pointer that is empty can be tested. An uninitialized pointer cannot even be tested safely, because the test itself reads the indeterminate value. nullptr is the spelling that turns "not set yet" into a value the program can reason about.
Checklist
- Before taking an address, decide which operator you need:
&takes the address of an object,*follows a pointer back to the object, and the two are not interchangeable. - Before choosing between a pointer and a reference, check whether the name must ever be empty or re-pointed: a reference is always bound and never empty, a pointer can be both.
- Before assigning to a reference, remember that the assignment writes through it:
r = b;changesa, the objectralready names, and does not rebindrtob. - Before passing an object to a function that must change it, check the parameter type:
int ncopies,int& nwrites to the caller's object,const int& nreads it without changing it. - Before doing pointer arithmetic, check that the pointer stays inside its array:
q + 2on a three-element array is the last element,q + 3is one past the end and must not be dereferenced. - Before writing a null pointer, write
nullptr:NULLand0still compile, but onlynullptrcan never be mistaken for an integer. - Before dereferencing a pointer that can be empty, check it against
nullptr: the check costs one comparison and removes the whole class of failure. - Before returning the address of a local variable, stop: the local goes out of scope, the pointer dangles, and the fix is to return a value or a smart pointer instead.
- Before using a pointer, check that it was initialized:
int* p = nullptr;is empty on purpose and can be tested, while a bareint* p;holds an indeterminate value that cannot even be tested safely.
Related guides on this site
- C++ Variables and Data Types: Declaration, Initialization, and Sizes — the previous guide in this foundation series, where
boolprints as1, brace initialization rejects narrowing, and an uninitialized variable earns a warning. - C++20 Modules with CMake and Ninja — the first course guide, where modules replace headers.
- Migrating a Template Library from SFINAE to C++20 Concepts — the second course guide, where concepts replace
enable_if. - Modern C++ Guides: C++20 and C++23, Built and Run — the index of all nineteen Modern C++ guides on this site.
Sources
- cppreference, Pointer declaration — a pointer that points to an object represents the address of the first byte in memory occupied by the object; indirection through an invalid pointer value is undefined behavior; addition and subtraction are defined for pointers to elements of arrays; pointers to elements of the same array compare in index order.
- cppreference, Reference declaration — a reference is required to be initialized to refer to a valid object or function.
- cppreference, nullptr, the pointer literal —
nullptris a prvalue of typestd::nullptr_t, with implicit conversions to the null pointer value of any pointer type;NULLand0are also null pointer constants, with the template case where they lose that meaning. - learncpp, Introduction to pointers — structure reference for how the topic is taught to beginners; all facts in this guide are grounded in the cppreference pages above and the box runs, not in this page.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.