C++ Pointers: Address-of and Dereference Operators for Everyone
Scared of pointers? Don't be! In this step-by-step guide, we explain memory addresses, the & operator, and the * asterisk.
C++ Pointers: From Zero to Hero (Step-by-Step)
If bitwise operations were like looking inside a lightbulb, then pointers are like a map of the giant hotel that is your computer's memory. Many people consider pointers the hardest part of C++, but I promise you – if you can write a friend's address on a piece of paper, you can understand pointers.
1. Imagine RAM Memory
Imagine your computer's RAM is a giant street with numbered houses.
- Inside each house lives a value (e.g., the number 42).
- Each house has its own unique number (address) (e.g., 1024 Byte St.).
Normally, when you create a variable: int age = 25;, the computer doesn't care about the name "age". It simply reserves a house at a specific address and puts the number 25 inside.
2. The Address-of Operator (&) – "Where do you live?"
The ampersand operator (&) is your detective tool. When you put it in front of a variable name, you don't get its content, but its memory address.
Example:
int treasure = 100;
std::cout << treasure << std::endl; // Prints: 100
std::cout << &treasure << std::endl; // Prints something like: 0x7ffcc (This is the address!)This is the key moment. &treasure is the information: "The treasure is in room 0x7ffcc".
3. What is a Pointer? (Your slip of paper)
A pointer is simply a special variable used to store... addresses. Nothing more. It's like a "post-it note" where you wrote down a house number.
How do you create one? We use the asterisk (*) during type declaration:
int* map; // This is a pointer to an integerNow we can store the address of our variable there:
int age = 30;
int* agePointer = &age; // We save the address of 'age' on our paper4. The Dereference Operator (*) – "Go to that address"
This is where the magic and the most confusion begins. The same asterisk (*) used to create a pointer is also used for dereferencing.
Dereferencing is simply going to the address written on the paper and checking what's inside (or changing it).
Example of use:
int number = 10;
int* p = &number;
// Printing the value via the pointer
std::cout << *p << std::endl; // Prints: 10
// Changing the value via the pointer!
*p = 20;
std::cout << number << std::endl; // Prints: 20! Do you see that? We didn't change the number variable directly. We went to the address stored in p and performed a "renovation" there.
5. Summary of the difference: & vs *
This is the moment you need to focus. This is the most common mistake for beginners.
| Operator | Name | What does it do? | Analogy |
|---|---|---|---|
& | Address-of | Gets the variable's address. | Asking: "Where do you live?" |
* (at type) | Declaration | Creates a pointer variable. | Taking a blank paper for addresses. |
* (at variable) | Dereference | Accesses the content at the address. | Driving to the address and knocking. |
6. Why is this important?
You might ask: "Why do I need this if I can just use the variable name?".
- Efficiency: Passing an address to a function (4-8 bytes) is faster than copying a huge object that takes up e.g., 1MB.
- Dynamic Memory: Without pointers, you couldn't create arrays whose size changes during program execution.
- Hardware Access: C++ allows you to look directly at processor registers using pointers.
7. Pitfalls, or how not to crash your program
Pointer to nothing (nullptr)
Never leave a pointer "roaming free". If you don't have an address yet, assign it nullptr.
int* p = nullptr; // Safe
// *p = 10; // THIS WILL CRASH THE PROGRAM! Always check if p != nullptrDangling pointer
This is a pointer that points to an address that no longer exists (e.g., a local variable was deleted). It's like trying to enter a house that has been demolished.
Your first program with pointers
Try to analyze this code:
#include <iostream>
void addTwo(int* numberPtr) {
if (numberPtr != nullptr) {
*numberPtr = *numberPtr + 2;
}
}
int main() {
int myData = 10;
std::cout << "Before: " << myData << std::endl;
addTwo(&myData); // We send the ADDRESS
std::cout << "After: " << myData << std::endl; // Prints 12!
return 0;
}Congratulations! You've just worked through the most difficult concept in C++. Pointers are simply GPS for your data.
Would you like me to explain pointer arithmetic next (how to navigate arrays using p++)?
You might also like
Stage 5: References and References as Function Parameters
Understanding the reference mechanism in C++: how variable aliases work and why they are key to efficient argument passing.
C++ String and Number Conversion: Deep Dive into sprintf and strtof
A comprehensive guide to classic conversion methods in C++. Learn how to safely use sprintf and strtof, and explore their modern alternatives.
Dynamic Memory Allocation in C++: Building and Demolishing in RAM
Understand the new and delete operators. We explain the difference between the stack and the heap, how to manage memory, and avoid memory leaks.