Lesson 4 of 8Article18 min
Pointers and References
Pointers hold addresses and can be reassigned. References are aliases to existing objects and must be initialized immediately.
01Pointer and reference differences
Pointers hold addresses and can be reassigned. References are aliases to existing objects and must be initialized immediately.
Pointers and references in action
#include <iostream>
using namespace std;
int main() {
int value = 10;
int* ptr = &value;
int& ref = value;
*ptr = 20;
ref = 30;
cout << value << endl; // 30
}02Memory safety mindset
- Prefer smart pointers over raw new/delete in modern C++.
- Use const references to avoid unnecessary copies.
- Avoid dangling pointers by understanding object lifetimes.