🎯 Pointers in C++ — Addresses, Dereferencing and Arithmetic
Master C++ pointers — addresses, dereferencing, pointer arithmetic, nullptr, pointer vs array decay, and the dangers of dangling pointers. Beginner-friendly examples + interview Q&A.
A pointer is a variable that stores the memory address of another variable. They're powerful, dangerous, and the most common reason 'C++ is hard.'
🧠 The mental model
Imagine memory as a long row of mailboxes, each with an address. A normal variable is a value inside a mailbox. A pointer is a slip of paper with the address of another mailbox written on it.
📜 Syntax
int x = 42;
int* p = &x; // p stores the address of x
std::cout << p; // 0x7ffe... (the address)
std::cout << *p; // 42 (the value at that address)
*p = 100; // changes x via the pointer
std::cout << x; // 100🔑 Key operators
&x— address-of operator*p— dereference operator
⚠️ nullptr — the safe 'no address yet'
int* p = nullptr;
if (p != nullptr) {
std::cout << *p; // safe
}➕ Pointer arithmetic
int arr[] = {10, 20, 30};
int* p = arr; // points to arr[0]
std::cout << *(p + 1); // 20💻 Code Examples
Pointer basics
int x = 5;
int* p = &x;
std::cout << "x = " << x << '\n';
std::cout << "&x = " << &x << '\n';
std::cout << "p = " << p << '\n';
std::cout << "*p = " << *p << '\n';
*p = 99;
std::cout << "x now = " << x;Output:
x = 5 &x = 0x7ffe... p = 0x7ffe... *p = 5 x now = 99
Swap via pointers
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int x = 1, y = 2;
swap(&x, &y);
std::cout << x << ' ' << y;Output:
2 1
Dangling pointer — DON'T
int* badPointer() {
int local = 42;
return &local; // ❌ local dies when function ends
}Output:
Caller dereferences garbage — undefined behavior.
⚠️ Common Mistakes
- Dereferencing a null or uninitialized pointer — crash or undefined behavior.
- Returning a pointer to a local variable — that memory is gone after the function returns.
- Confusing `int* p, q;` — only p is a pointer, q is an int! Write `int *p, *q;` or each on its own line.
- Using `NULL` (an old C macro) instead of `nullptr` — prefer `nullptr` (typed, safer).
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is a pointer?Beginner
A variable that stores the memory address of another variable. Declared with `Type*`. Access the address with `&var`, the value at an address with `*ptr` (dereference).
Q2.What's the difference between `int* p` and `int** p`?Beginner
`int*` is a pointer to an int. `int**` is a pointer to a pointer to an int — used when you want to modify what a pointer points to from another function.
Q3.What is a dangling pointer?Intermediate
A pointer that points to memory that has been freed or to a local variable that has gone out of scope. Dereferencing it is undefined behavior — possibly garbage data or a crash.
Q4.What is the difference between a pointer and a reference?Intermediate
Pointer: a separate variable holding an address; can be reassigned; can be null. Reference: an alias for an existing variable; must be initialized at declaration; cannot be reassigned or null. References are safer and preferred when 'optional' isn't needed.
Q5.What does pointer arithmetic actually do?Advanced
Adding N to a pointer of type T moves it forward by N*sizeof(T) bytes — not just N bytes. So `int* p; p+1` advances 4 bytes (assuming int is 4). This is why pointer arithmetic respects array layout.
🧠 Quick Summary
- Pointer = variable holding a memory address.
- `&x` gets the address; `*p` dereferences (reads/writes the value).
- Use `nullptr` instead of NULL or 0.
- Arrays decay to pointers; `arr[i]` is `*(arr + i)`.
- Watch for null, uninitialized, and dangling pointers.