What are Pointers in C++?
Learn what pointers in C++ are, how the & and * operators work, dynamic memory with new/delete, and why modern C++ prefers smart pointers.
Expected Interview Answer
A pointer in C++ is a variable that stores the memory address of another variable, letting you access and modify that data indirectly through its location rather than its name.
You declare a pointer with the type followed by an asterisk, take an address with the address-of operator &, and read or write the pointed-to value by dereferencing with *. Pointers enable dynamic memory allocation with new/delete, efficient passing of large objects, building linked data structures, and interfacing with hardware and arrays. Because raw pointers can dangle, leak, or be null, modern C++ favors smart pointers (unique_ptr, shared_ptr) and references for safer ownership and lifetime management.
- Enable dynamic memory allocation at runtime
- Allow efficient passing of large data without copying
- Support building linked lists, trees, and graphs
- Permit direct access to memory and hardware
- Underpin polymorphism through base-class pointers
- Form the basis for smart pointers and ownership models
AI Mentor Explanation
A pointer is like a scoreboard slot that does not hold a run tally itself but displays which player is currently on strike. Follow the reference to the named batsman and you find the real innings data, just as dereferencing a pointer leads you to the actual value in memory.
Step-by-Step Explanation
Step 1
Declare a pointer
Use the type with an asterisk, e.g. int* p, meaning p can hold the address of an int.
Step 2
Take an address
Assign an address with the address-of operator, e.g. p = &value.
Step 3
Dereference
Access or modify the pointed-to value with the dereference operator, e.g. *p = 10.
Step 4
Allocate dynamically
Use new to allocate on the heap and delete to free it, avoiding leaks.
Step 5
Prefer smart pointers
In modern C++, use unique_ptr or shared_ptr so memory frees automatically and safely.
What Interviewer Expects
- Defining a pointer as a variable holding a memory address
- Explaining the & and * operators correctly
- Distinguishing a null pointer from a dangling pointer
- Awareness of dynamic allocation with new/delete
- Knowing modern C++ prefers smart pointers and references
Common Mistakes
- Confusing the pointer with the value it points to
- Dereferencing a null or uninitialized pointer
- Forgetting to delete heap memory, causing leaks
- Mixing up new/delete with new[]/delete[]
- Assuming pointers and references are identical
Best Answer (HR Friendly)
“A pointer is a variable that stores the address of where another value lives in memory, rather than the value itself. It lets programs work with data indirectly, which is useful for dynamic memory and efficient handling of large data, though modern C++ often uses safer smart pointers.”
Code Example
#include <iostream>
#include <memory>
int main() {
int value = 42;
int* p = &value; // p holds the address of value
std::cout << *p << "\n"; // dereference: prints 42
*p = 100; // modify value through the pointer
std::cout << value << "\n"; // prints 100
// Modern C++: smart pointer frees memory automatically
std::unique_ptr<int> up = std::make_unique<int>(7);
std::cout << *up << "\n"; // prints 7
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 it?
- What is a null pointer and how do you check for it?
- How do unique_ptr and shared_ptr differ?
- What is pointer arithmetic and when is it used?
MCQ Practice
1. What does a pointer variable store?
A pointer stores the memory address of another variable, not the value itself.
2. Which operator retrieves the value a pointer points to?
The dereference operator * accesses the value stored at the address the pointer holds.
3. Which smart pointer expresses exclusive ownership of a resource?
unique_ptr owns its resource exclusively and cannot be copied, freeing memory automatically on scope exit.
Flash Cards
What is a pointer? — A variable that stores the memory address of another variable.
What does the & operator do? — It returns the memory address of a variable (address-of).
What does the * operator do on a pointer? — It dereferences the pointer, accessing the value at the stored address.
What is a dangling pointer? — A pointer referencing memory that has already been freed or gone out of scope.
Why prefer smart pointers? — They manage memory automatically via RAII, preventing leaks and dangling pointers.