Pointers Basics in C++
A pointer is a variable holding a memory address: int* p = &x; stores x's address in p, *p reads or writes x through it, and & takes addresses. This indirection powers dynamic allocation, polymorphic dispatch, and data structures built from linked nodes — plus the entire C ecosystem.
The discipline is non-negotiable: initialize every pointer (nullptr when no target yet), check for nullptr before dereferencing, and never use a pointer after its pointee dies. Modern C++ pushes raw pointers to non-owning roles, with smart pointers owning — but you must master raw mechanics first.
& takes, * follows
&x is x's address; if p holds it, *p is x itself. & and * are inverse operations across the declaration/use divide.
Declare carefully
int* p reads as pointer-to-int. Write int* p (not int *p) and declare one pointer per line — int* a, b makes b a plain int.
nullptr, always initialized
Pointers start life as garbage like ints do — int* p = nullptr; marks empty explicitly and crashes loudly (not silently) if misused.
Never dangle
A pointer to a destroyed local or freed block is a time bomb. Null it, reassign it, or scope it — never dereference the dead.
#include <iostream> int main() { int x = 10; int* p = &x; *p = 20; std::cout << x << ", " << *p << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.