C++ Tutorial: Pointers & References
Memory addresses, pointer declaration, dereferencing, nullptr, pointer arithmetic, and references — C++'s most distinctive feature.
The heart of C++
Pointers are what make C++ different from every other language. A pointer is just a variable that holds a memory address — instead of a value, it holds a location.
This is what lets C++ manipulate hardware directly, write operating systems, and achieve performance impossible in garbage-collected languages. Pointers are scary at first. By the end of this chapter, they'll be your superpower.
What you'll learn in this C++ pointers & references tutorial
This interactive C++ tutorial has 7 hands-on exercises. Estimated time: 22 minutes.
- What is a pointer? — A pointer holds a **memory address** — the location of another variable.
- nullptr — the safe null pointer — A pointer that points to nothing should be set to `nullptr` (C++11).
- Pointer arithmetic — You can do arithmetic on pointers — adding 1 moves to the NEXT element of that type:
- const with pointers — Two kinds of const with pointers:
- References vs pointers — References are safer, simpler aliases:
- Pointers to strings and arrays — Array names are pointers to their first element. C-strings are `const char*` — a pointer to the first character.
- Build a pointer toolkit — No starter code.
C++ Pointers & References concepts covered
- The heart of C++