CurriculumPointers

Pointers

A pointer is simply a variable that stores the memory address of another variable. While variables hold values (like 42 or 'A'), pointers hold locations (like 0x7ffee21). Pointers give C++ its incredible performance and hardware-level control.

01The Address-of Operator (&)

Every variable lives somewhere in your computer's RAM. We can find exactly where it lives using the & operator.

Run this code. The output will look like 0x... which is a hexadecimal memory address.
main.cpp
Loading...
Terminal
Waiting for execution...
Visualizer
Run the code to see memory.

02Creating a Pointer

To store an address, we need a special variable called a Pointer. You declare it by putting an * next to the type.

A pointer to an int is declared as int*.
main.cpp
Loading...
Terminal
Waiting for execution...
Visualizer
Run the code to see memory.

03Dereferencing (*)

Once you have a pointer, you can access the actual value at that address using the * operator again. This is called dereferencing.

You can also use it to change the value. Modifying *ptr modifies score directly!
main.cpp
Loading...
Terminal
Waiting for execution...
Visualizer
Run the code to see memory.

04Null Pointers

If you declare a pointer but don't give it an address, it will point to random garbage memory. Always initialize pointers to nullptr if you don't have an address yet.

Dereferencing a nullptr causes an immediate crash (Segmentation Fault).
main.cpp
Loading...
Terminal
Waiting for execution...

05Pointers and Arrays

In C++, an array name is actually just a pointer to its first element! Because elements are stored side-by-side in memory, you can add 1 to the pointer to jump to the next element.

This is called Pointer Arithmetic.
main.cpp
Loading...
Terminal
Waiting for execution...
← Back to Curriculum