What is a Pointer in C++?
Learn what a pointer is in C++, how it stores memory addresses, dereferencing, dangling pointers, and why smart pointers are preferred today.
Expected Interview Answer
A pointer is a variable that stores the memory address of another variable, letting you access or modify that variable indirectly through the address rather than its name.
Pointers are declared with a type and an asterisk, such as `int* p`, and are assigned an address using the address-of operator `&`. Dereferencing with `*p` reads or writes the value stored at that address. Pointers underpin dynamic memory allocation, linked data structures, and passing large objects efficiently without copying. In modern C++, raw pointers for ownership are largely replaced by smart pointers (`unique_ptr`, `shared_ptr`) which manage the lifetime automatically, though raw pointers still appear for non-owning references. Uninitialized or dangling pointers are a common source of undefined behavior.
- Enables indirect access and modification of memory
- Supports dynamic memory allocation and data structures
- Avoids expensive copies when passing large objects
- Foundation for arrays, strings, and function pointers
- Smart pointers add automatic, safe lifetime management
AI Mentor Explanation
A pointer is like the seating chart number on a stadium ticket rather than the seat itself — it tells you exactly where to go without being the seat. Handing someone the chart entry lets them find your exact spot instantly instead of describing the whole stand. If the seat gets removed and the entry still points there, you have a dangling reference to nothing.
Step-by-Step Explanation
Step 1
Declare the pointer
Write `int* p;` to declare a pointer that will hold the address of an int.
Step 2
Take an address
Use `p = &x;` so the pointer stores the memory address of variable `x`.
Step 3
Dereference to access
Use `*p` to read or write the value stored at the address `p` holds.
Step 4
Prefer smart pointers for ownership
Use `std::unique_ptr` or `std::shared_ptr` when the pointer owns dynamically allocated memory, so cleanup is automatic.
Step 5
Avoid dangling pointers
Never dereference a pointer after the object it points to has been destroyed or freed.
Step 6
Check before use
Compare against `nullptr` before dereferencing to avoid undefined behavior on null pointers.
What Interviewer Expects
- Explains pointer as an address-holding variable
- Knows the difference between `&` (address-of) and `*` (dereference)
- Can distinguish raw pointers from smart pointers
- Understands dangling and null pointer risks
- Mentions pointer use in dynamic memory and data structures
Common Mistakes
- Confusing a pointer with the value it points to
- Forgetting to initialize a pointer before use
- Dereferencing a pointer after the pointed-to object is freed
- Using raw `new`/`delete` instead of smart pointers for ownership
- Not checking for `nullptr` before dereferencing
Best Answer (HR Friendly)
“A pointer is a variable that stores the location of another piece of data, similar to an address rather than the data itself. It lets a program work with data efficiently without copying it around, though modern C++ tools help manage this safely.”
Code Example
#include <iostream>
int main() {
int x = 42;
int* p = &x; // p holds the address of x
std::cout << "Value: " << *p << "\n"; // Value: 42
*p = 100; // modifies x through p
std::cout << "Updated x: " << x << "\n"; // Updated x: 100
return 0;
}#include <memory>
#include <iostream>
int main() {
std::unique_ptr<int> up = std::make_unique<int>(7);
std::cout << *up << "\n"; // 7
// Memory is freed automatically when up goes out of scope
return 0;
}Follow-up Questions
- What is the difference between a pointer and a reference?
- What is a dangling pointer and how do you avoid one?
- How do `unique_ptr` and `shared_ptr` differ?
- What does `nullptr` mean and how is it different from `NULL`?
- What is pointer arithmetic and when is it valid?
MCQ Practice
1. What does a pointer variable store?
A pointer stores the memory address of another variable, not a copy of its value.
2. Which operator dereferences a pointer to access its pointed-to value?
The `*` operator dereferences a pointer, giving access to the value stored at the address it holds.
3. What is a dangling pointer?
A dangling pointer still holds an address after the memory it pointed to has been freed or gone out of scope, so dereferencing it is undefined behavior.
Flash Cards
What does a pointer store? — The memory address of another variable, not the value itself.
How do you get a variable's address? — Use the address-of operator `&`, e.g. `p = &x;`.
How do you access the value a pointer points to? — Dereference it with `*p`.
What is preferred over raw owning pointers in modern C++? — Smart pointers like `std::unique_ptr` and `std::shared_ptr`, which manage lifetime automatically.