What is nullptr?
C++11 null pointer literal—type-safe and preferred over NULL macro.
Master C++ pointers with comprehensive examples of all pointer types, memory management, smart pointers, pointer arithmetic, and practical applications. Learn efficient memory manipulation techniques.
Memory addresses
Complex types
Memory safety
Memory navigation
Pointers hold addresses—powerful and error-prone. This chapter demystifies *, &, nullptr, and pointer arithmetic so you can read systems code and interview answers confidently.
Pointers separate C++ beginners from engineers. They appear in linked lists, callbacks, and performance-critical APIs.
Pointers are powerful features of C++ that store memory addresses. They enable direct memory access, dynamic memory allocation, and efficient data manipulation. Mastering pointers is essential for advanced C++ programming.
Stores memory address of a variable. Foundation of pointer concepts.
Pointers to pointers, function pointers, void pointers for flexibility.
Automatically manage memory, prevent leaks (C++11 and later).
Navigate through memory using arithmetic operations on pointers.
int x = 42;0x7fff5fbff8ac
int* ptr = &x;0x7fff5fbff8acPointer stores memory address: ptr contains the address of x, allowing indirect access to its value.
int x = 42;
int* p = &x;cout << *p; // 42*p = 100;
cout << x;cout << &p;int* q = nullptr;
if (!q) cout << "null";int a=1,b=2;
p=&a; p=&b;nullptr if not pointing to valid memory)const appropriately to prevent accidental modificationsThe following table compares all pointer types in C++ with their syntax, use cases, and characteristics:
| Pointer Type | Syntax | When to Use | Characteristics |
|---|---|---|---|
| Basic Pointer | int* ptr; | Direct memory access, dynamic allocation | Stores address, can be reassigned |
| Pointer to Pointer | int** ptr; | Dynamic 2D arrays, function arguments | Points to another pointer, double indirection |
| Void Pointer (Generic) | void* ptr; | Generic functions, memory operations | Can point to any type, cannot be dereferenced directly |
| Function Pointer | int (*funcPtr)(int, int); | Callbacks, event handlers, strategy pattern | Points to function, enables runtime function selection |
| Array Pointer | int (*arrPtr)[10]; | Pointer to entire array, multi-dimensional arrays | Different from pointer to first element |
| Const Pointer | int* const ptr; | Pointer that cannot be reassigned | Constant pointer, variable data |
| Pointer to Const | const int* ptr; | Read-only access to data | Variable pointer, constant data |
| unique_ptr (C++11) | unique_ptr<int> ptr; | Single ownership, resource management | Automatically deletes, cannot be copied |
| shared_ptr (C++11) | shared_ptr<int> ptr; | Shared ownership, reference counting | Automatically deletes when last reference goes |
| weak_ptr (C++11) | weak_ptr<int> ptr; | Break circular references | Non-owning reference to shared_ptr |
| this Pointer | this | Within class methods to access members | Implicit pointer to current object |
int x=10;
int* p=&x;
int** pp=&p;cout << **pp;**pp = 99;
cout << x;int** m = new int*[rows];const char* names[]={"A","B"};if (pp && *pp) cout << **pp;int n=5;
void* vp = &n;int* ip = static_cast<int*>(vp);
cout << *ip;void* buf = malloc(16);free(buf); buf=nullptr;float f=1.2f;
vp = &f;cout << sizeof(int*);std::any (C++17)int (*fp)(int,int) = add;cout << fp(3,4);using Fn = int(*)(int,int);
Fn f = add;apply(5, 2, mul);Fn ops[] = {add, sub};if (fp) fp(1,1);auto p = make_unique<int>(42);auto a = make_shared<int>(10);
auto b = a;weak_ptr<int> w = a;} // unique_ptr frees memorya.reset();
b.use_count();// prefer make_uniqueint arr[]={1,2,3};
int* p=arr;
p++; cout<<*p;cout << *(arr+2);int* e = arr+3;
cout << e-arr;char* s="hi";
while(*s) s++;const char* msg = "OK";// do not pass end+1n to a pointer moves it forward by n * sizeof(type) bytesn from a pointer moves it backward by n * sizeof(type) bytes| Common Error | Example | Solution |
|---|---|---|
| Dereferencing Null Pointer | int* ptr = nullptr; *ptr = 5; |
Always check for null before dereferencing |
| Dangling Pointer | int* ptr = new int; delete ptr; *ptr = 10; |
Set pointer to null after deletion, use smart pointers |
| Memory Leak | int* ptr = new int[100]; // No delete |
Always match new with delete, use RAII/smart pointers |
| Buffer Overflow | int arr[5]; int* p = arr + 10; |
Check bounds, use containers like vector |
| Type Mismatch | float* ptr = (float*)&intVar; |
Avoid unsafe casts, use proper type conversions |
| Pointer Arithmetic on Wrong Type | void* ptr; ptr++; |
Don't use arithmetic on void pointers |
| Returning Local Variable Address | int* func() { int x; return &x; } |
Don't return address of local variables |
| Double Free | delete ptr; delete ptr; |
Set pointer to null after deletion |
nullptr for empty)const whenever possiblenew with exactly one deleteIn modern C++ (C++11 and later), prefer smart pointers (unique_ptr, shared_ptr) for ownership, use raw pointers only for non-owning references, and leverage references when null isn't needed. Use std::array or std::vector instead of raw arrays.
int arr[] = {1, 2, 3, 4, 5};
int* ptr = arr + 2;
cout << *(ptr - 1) + *(ptr + 1);
& to get address, * to dereferenceunique_ptr: Exclusive ownership, cannot be copiedshared_ptr: Shared ownership with reference countingweak_ptr: Non-owning reference to break circular dependenciesmake_unique and make_shared for creationthis pointer: Implicit pointer to current objectn moves pointer by n * sizeof(type)Mastering pointers is crucial for advanced C++ programming. Practice with pointer-based data structures (linked lists, trees), explore memory management patterns, learn about move semantics (C++11), and study the Standard Template Library (STL) containers that internally use pointers. Understanding pointers deeply will make you a more effective C++ programmer.
C++11 null pointer literal—type-safe and preferred over NULL macro.
Generally no—only pointer ± integer or pointer − pointer (same array) is defined.
Pointer to freed or out-of-scope memory—undefined behavior if dereferenced.