Unit 3: Memory Management, Pointers & ReferencesLesson #9 / 20

Lesson 9: Master C++ Pointers, Memory Addresses (&) and Dereferencing (*)

NSNextSemLast Updated: 5 Aug, 2026

🎯 Pointers & Memory Allocation in C++

A Pointer is a variable whose value is the memory address of another variable. Understanding pointers gives you direct access to RAM and raw memory control!


🔑 Essential Pointer Operators

  • 📍 Address-of Operator (&): Returns the physical memory address of a variable (e.g. &x yields 0x7ffeefbff59c).
  • 🔓 Dereference Operator (): Used in pointer variable declaration (int *ptr) AND to access/modify the value stored at the target address (*ptr = 100;).
  • 🚫 nullptr: Modern C++11 keyword representing a null pointer (always initialize unassigned pointers to nullptr to prevent garbage memory bugs).

  • 💻 Code Example: Pointers in Action

    C++ Source Code (main.cpp)
    #include <iostream>
    int main() {
    int val = 250;
    int *ptr = &val; // ptr stores the memory address of val
    std::cout << "Value of val: " << val << std::endl;
    std::cout << "Memory Address (&val): " << &val << std::endl;
    std::cout << "Pointer value (ptr): " << ptr << std::endl;
    std::cout << "Dereferenced (*ptr): " << *ptr << std::endl;
    // Mutate original value via pointer dereferencing
    *ptr = 500;
    std::cout << "
    After mutating *ptr = 500:" << std::endl;
    std::cout << "New value of val: " << val << std::endl;
    return 0;
    }
    Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
    Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)

    Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

    (c) Microsoft Corporation. All rights reserved.

    C:\Users\Student\CPP_Project>#include <iostream>

    C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp C:\Users\Student\CPP_Project> main.exe Hello World from C++ on Windows 11 Pro! [Process exited with code 0 in 0.001 seconds]

    > 💡 Developer Joke: Why are pointers like secrets?

    > Because if you dereference a null pointer, your whole world crashes! 😂

    Interactive Knowledge Check

    Test your understanding of Lesson #9 concepts

    If int x = 42; int *ptr = &x;, what does *ptr evaluate to?