Advanced 18 min readModule: Module 3: Pointers, References & Pointer Arithmetic
Memory Addresses, Pointers & References
Master direct memory addressing (&), pointer dereferencing (*), null pointers (nullptr), and references.
What You Will Learn in This Lesson
- How pointers store memory addresses of variables
- Dereferencing pointers to mutate underlying memory (*ptr = val)
- References (const Type&) for zero-copy function parameter passing
Introduction & Core Concept
A pointer is a variable that stores the memory address of another variable. References provide safe aliases to existing variables without nullability risks.
WHY DOES THIS MATTER IN THE REAL WORLD?
Passing 1MB objects by value makes an expensive copy. Passing by const reference (const BigObject&) takes 0 copies (8 bytes pointer).
Pointer & Reference Manipulation
cppcpp
123456789101112#include <iostream>void increment(int& ref) { ref += 1; }int main() {int value = 10;int* ptr = &value;*ptr = 20; // Mutate via pointerincrement(value); // Mutate via referencestd::cout << "Final Value: " << value << " at address: " << ptr << std::endl;return 0;}
Line-by-Line Technical Breakdown
1Always use nullptr instead of NULL or 0 for uninitialized pointers.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[CPP]
CPP SOURCE EDITOR
Interactive Live CodeIndustry Best Practices & Professional Standards
- Pass large structs and vectors by const reference (const std::vector<int>&).
Lesson Summary & Core Takeaways
- Pointers and references give direct control over computer memory.