Pointers
A pointer is an object that stores the address of another object (or a function). You dereference it with * to reach the thing it points at, and you may reseat it, set it to nullptr, or walk it through an array.
Detailed Explanation
A reference is a permanent nickname. A pointer is a variable that holds a location. That extra object is why pointers can be null, can be stored in arrays, and can change which object they talk to. It is also why they are the type you use for optional, reseatable, or heap-allocated things.
The declaration int* p = &n; reads "p is a pointer to int, initialised with the address of n". p itself occupies one pointer-sized slot (8 bytes on a 64-bit ABI). *p is the int at that address. p = &m; makes it point at a different int. p = nullptr; makes it point at nothing.
nullptr is a keyword of type std::nullptr_t. It converts to any pointer type. It replaced NULL and 0, both of which are integers and pick the wrong overload when a function has an int parameter and a pointer parameter.
Pointers do arithmetic. p + 1 advances by sizeof(T) bytes, not by one byte. That rule is how arrays work: a[i] is defined as *(a + i). Arithmetic is only defined inside an array (or one-past-the-end). Adding to a pointer that points at a single stack int is undefined behaviour.
A pointer can point at const: const int* p means "I may not write through this pointer". int* const p means "this pointer variable cannot be reseated". const int* const p is both. Read those declarations from right to left.
Function pointers store the address of a function: int (*fn)(int, int) = &add;. You call through them with fn(1, 2). They are how C-style callbacks work. In modern C++ a lambda that does not capture converts to a function pointer; a capturing lambda does not, and you store it in std::function or a template parameter instead.
Void pointers (void*) erase the type. You may not dereference or increment a void*. You cast it back to the real type first. They show up in C APIs (malloc, qsort). Prefer templates or std::any when you control the code.
Owning raw pointers are the danger. new int returns a pointer you must later delete. If you copy that pointer, two sites think they own the same allocation. The modern default is std::unique_ptr for exclusive ownership and std::shared_ptr for shared ownership. Raw pointers remain the right type for a non-owning observer: a function that receives Widget* is saying "I will use this, I will not delete it".
A dangling pointer is a pointer whose object has died. Stack objects die at the end of their scope. Heap objects die at delete. Reading or writing through a dangling pointer is undefined behaviour.
Code Examples
#include <iostream>
int add(int a, int b) { return a + b; }
int main() {
int n = 7;
int* p = &n;
*p = 8; // n is now 8
std::cout << n << '\n'; // prints: 8
p = nullptr;
if (p == nullptr) {
std::cout << "empty\n"; // prints: empty
}
int (*fn)(int, int) = add; // & is optional for functions
std::cout << fn(2, 3) << '\n'; // prints: 5
}#include <cstddef>
#include <iostream>
int main() {
int scores[] = {10, 20, 30};
int* p = scores; // array decays to pointer to first element
for (std::size_t i = 0; i < 3; ++i) {
std::cout << *(p + i) << ' ';
}
std::cout << '\n'; // prints: 10 20 30
}Common Gotchas
Dereferencing `nullptr` is undefined behaviour
`int* p = nullptr; *p = 1;` is not a guaranteed crash. The compiler may delete nearby checks. Test for null before every dereference that is not already proven non-null.
`delete` does not change the pointer
after `delete p;`, `p` still holds the old address. Set `p = nullptr` if the variable lives on, or better, wrap the allocation in `std::unique_ptr`.
Prefer references when null is not allowed
a `T&` parameter documents "this exists". A `T*` parameter documents "this might be absent, or I need to reseat it".