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
Run this code. The output will look like
& operator.Run this code. The output will look like
0x... which is a hexadecimal memory address.main.cpp
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
A pointer to an
* next to the type.A pointer to an
int is declared as int*.main.cpp
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
You can also use it to change the value. Modifying
* operator again. This is called dereferencing.You can also use it to change the value. Modifying
*ptr modifies score directly!main.cpp
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
Dereferencing a
nullptr if you don't have an address yet.Dereferencing a
nullptr causes an immediate crash (Segmentation Fault).main.cpp
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.
This is called Pointer Arithmetic.
main.cpp
Terminal
Waiting for execution...
AI Tutor
Pointers are the core of C++. Without them, you cannot build dynamic data structures (trees, graphs), pass large objects efficiently, or interact with the operating system.