C++ inherits pointers from C: variables that store addresses. Use pointers for optional/nullable indirection, C APIs, and low-level code—but prefer references and smart pointers in application logic.
Basics
int x = 42;
int* p = &x;
std::cout << *p << "\n"; // 42
Use nullptr instead of NULL or 0 for null pointers in modern C++.
Important interview questions and answers
- Q: Pointer vs reference?
A: Pointers can be null and reseated; references must bind to valid objects. - Q: When still use raw pointers?
A: Non-owning observers, C interop, and legacy APIs—document ownership clearly.
Self-check
- What does *p do?
- Preferred null pointer constant in C++11+?
Tip: Use nullptr not NULL or 0—compare with C pointer habits but modernize null checks.
Interview prep
- What is nullptr?
Type-safe null pointer constant in modern C++—prefer over NULL or 0.
- When use raw pointers?
Non-owning observers, C API interop, and legacy code—document ownership clearly.