Intermediate 8 min

Pointers

Variables that store memory addresses, allowing direct manipulation of data in memory.

Learn how pointers work in C++ - variables that store memory addresses, enabling powerful memory manipulation and dynamic data structures.

A Simple Example

#include <iostream>

int main() {
    int x{42};
    int* ptr{&x};  // ptr stores the address of x

    std::cout << "Value: " << *ptr << "\n";     // Output: 42
    std::cout << "Address: " << ptr << "\n";    // Output: memory address

    // Modify through pointer
    *ptr = 100;
    std::cout << "New value: " << x << "\n";    // Output: 100

    return 0;
}

Breaking It Down

Declaring Pointers with *

  • What it does: Declares a variable that holds a memory address
  • Syntax: int* ptr; declares a pointer to an integer
  • Remember: The asterisk * in declaration means "pointer to"
  • Type matters: int* ptr can only point to int variables

Address-of Operator &

  • What it does: Gets the memory address of a variable
  • Syntax: &x returns the address where x is stored
  • Use for: Initializing pointers to point at existing variables
  • Remember: Think of & as "where is this variable located?"

Dereference Operator *

  • What it does: Accesses the value at the address stored in the pointer
  • Syntax: *ptr gives you the value that ptr points to
  • Use for: Reading or modifying the value through the pointer
  • Remember: Think of * as "look inside the box at this address"

Null Pointers and Safety

  • What it is: A pointer that points to nothing (nullptr in modern C++)
  • Syntax: int* ptr{nullptr}; initializes a null pointer
  • Danger: Dereferencing a null pointer crashes your program
  • Remember: Always check if pointer is not nullptr before dereferencing

Why This Matters

  • Enables dynamic memory allocation for flexible data structures like linked lists, trees, and graphs
  • Critical for understanding how C++ manages memory under the hood and how function calls work
  • Foundation for advanced topics like smart pointers, references, and memory-efficient programming

Critical Insight

Pointers are like house addresses. The variable int x{42} is like a house containing the number 42. A pointer int* ptr{&x} is like writing down the house address.

When you use *ptr, you're saying "go to that address and look at what's inside." You can read it or change it. Multiple pointers can have the same address - like giving the same house address to different people!

The key insight: & gets the address, * uses the address. They're opposites that work together.

Best Practices

Always initialize pointers: Use int* ptr{nullptr}; or int* ptr{&x};. Uninitialized pointers point to random memory and cause crashes.

Check for nullptr before dereferencing: Use if (ptr != nullptr) before accessing *ptr to avoid crashes.

Use smart pointers for dynamic memory: Modern C++ has std::unique_ptr and std::shared_ptr that handle memory automatically.

Prefer references over pointers when possible: References are safer and clearer when you don't need to reassign or check for null.

Common Mistakes

Uninitialized pointers: Declaring int* ptr; without initialization points to random memory. Always initialize!

Dereferencing nullptr: Accessing *ptr when ptr is nullptr causes a crash. Always check first.

Pointer arithmetic confusion: ptr++ moves by sizeof(type) bytes, not 1 byte. An int pointer moves by 4 bytes.

Dangling pointers: After deleting or when variable goes out of scope, pointer still holds old address. Set to nullptr after deletion.

Debug Challenge

This code tries to use an uninitialized pointer. Click the highlighted line to fix the bug:

1 #include <iostream>
2
3 int main() {
4 int* ptr;
5 *ptr = 42;
6 std::cout << *ptr << "\n";
7 return 0;
8 }

Quick Quiz

  1. What does the & operator do?
Gets the address of a variable
Gets the value at an address
Creates a new pointer
Deletes a pointer
  1. What does the * operator do when used with a pointer?
Accesses the value at the memory address
Multiplies two numbers
Declares a pointer
Gets the address of a variable
  1. What should you initialize a pointer to if it doesn't point to anything yet?
nullptr
0
Nothing - leave it uninitialized
*

Practice Playground

Time to try out what you just learned! Play with the example code below, experiment by making changes and running the code to deepen your understanding.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once