int number = 99;Variable "number" stores the value:
number = 99
int* ptr = &number;ptrstores the address ofnumber&numbermeans the memory address ofnumber
ptr
โ
number
โ
99
*ptrMeaning: "Get the value from the address pointed to by ptr."
Result: 99
| Expression | Result |
|---|---|
ptr |
Address of number |
*ptr |
Value of number (99) |
int number = 99;
int* ptr = &number;
int** ptr2 = &ptr;ptr2
โ
ptr
โ
number
โ
99
| Expression | Result |
|---|---|
ptr |
Address of number |
*ptr |
99 |
ptr2 |
Address of ptr |
*ptr2 |
ptr |
**ptr2 |
99 |
int number = 1;
int* ptr = &number;
*ptr = 10;Result: number = 10
Because:
ptr
โ
number
So changing through *ptr directly changes the value of number.
int number = 1;
int* ptr = &number;
int** ptr2 = &ptr;
*ptr = 10;
**ptr2 = **ptr2 + *ptr;| Step | Action | Result |
|---|---|---|
| 1 | number = 1 |
number = 1 |
| 2 | *ptr = 10 |
number = 10 |
| 3 | **ptr2 = **ptr2 + *ptr |
10 = 10 + 10 |
| 4 | **ptr2 = 20 |
number = 20 |
Final Output: 20
Don't memorize. Follow the arrows:
ptr2
โ
ptr
โ
number
โ
99
Each * means: "Follow one more arrow."
| Expression | Meaning |
|---|---|
*ptr2 |
Follow one arrow โ ptr |
**ptr2 |
Follow two arrows โ 99 |
#include <iostream>
using namespace std;
int main() {
// Create a variable
int number = 99;
// Create a pointer to number
int* ptr = &number;
cout << "number = " << number << endl; // 99
cout << "ptr (address) = " << ptr << endl; // Memory address
cout << "*ptr (value) = " << *ptr << endl; // 99
// Create a pointer to pointer
int** ptr2 = &ptr;
cout << "\nptr2 (address of ptr) = " << ptr2 << endl; // Address of ptr
cout << "*ptr2 (ptr itself) = " << *ptr2 << endl; // Same as ptr
cout << "**ptr2 (value) = " << **ptr2 << endl; // 99
// Change value through pointer
*ptr = 50;
cout << "\nAfter *ptr = 50:" << endl;
cout << "number = " << number << endl; // 50
cout << "*ptr = " << *ptr << endl; // 50
cout << "**ptr2 = " << **ptr2 << endl; // 50
return 0;
}number = 99
ptr (address) = 0x7ffc8b1c4a4c
*ptr (value) = 99
ptr2 (address of ptr) = 0x7ffc8b1c4a50
*ptr2 (ptr itself) = 0x7ffc8b1c4a4c
**ptr2 (value) = 99
After *ptr = 50:
number = 50
*ptr = 50
**ptr2 = 50
Pointers are not monsters.
| Expression | Meaning |
|---|---|
ptr |
Address |
*ptr |
Value at the address |
ptr2 |
Address of ptr |
*ptr2 |
ptr |
**ptr2 |
Value at the address pointed to by ptr |
"Follow the arrows one by one."
Memory Address Variable Name Value
0x1000 โ ptr โ 0x2000
0x2000 โ number โ 99
Reading *ptr:
- Go to
ptrโ get0x2000 - Go to
0x2000โ get99
Memory Address Variable Name Value
0x3000 โ ptr2 โ 0x1000
0x1000 โ ptr โ 0x2000
0x2000 โ number โ 99
Reading **ptr2:
- Go to
ptr2โ get0x1000 - Go to
0x1000โ get0x2000 - Go to
0x2000โ get99
| Topic | Status |
|---|---|
| Basic Pointers (*) | โ Done |
| Pointer to Pointer (**) | โ Done |
| Pointer to Pointer to Pointer (***) | ๐ Next |
int x = 50;
int* p = &x;
cout << *p; // What does this print?Answer: 50
int x = 10;
int* p = &x;
*p = 20;
cout << x; // What does this print?Answer: 20
int x = 100;
int* p = &x;
int** pp = &p;
cout << **pp; // What does this print?Answer: 100
Last Updated: June 2026