Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

ย 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 

Repository files navigation

๐Ÿ“Œ Pointer Basics


Creating a Variable

int number = 99;

Variable "number" stores the value:

number = 99

Creating a Pointer

int* ptr = &number;

Meaning

  • ptr stores the address of number
  • &number means the memory address of number

Illustration

ptr
 โ†“
number
 โ†“
99

Dereference ("*")

*ptr

Meaning: "Get the value from the address pointed to by ptr."

Result: 99

Summary

Expression Result
ptr Address of number
*ptr Value of number (99)

๐Ÿ”— Pointer to Pointer

Creating a Double Pointer

int number = 99;
int* ptr = &number;
int** ptr2 = &ptr;

Illustration

ptr2
 โ†“
ptr
 โ†“
number
 โ†“
99

Understanding Pointer Layers

Expression Result
ptr Address of number
*ptr 99
ptr2 Address of ptr
*ptr2 ptr
**ptr2 99

โœ๏ธ Changing Value Through Pointer

int number = 1;
int* ptr = &number;
*ptr = 10;

Result: number = 10

Why?

Because:

ptr
 โ†“
number

So changing through *ptr directly changes the value of number.


๐Ÿงฎ Important Example

int number = 1;
int* ptr = &number;
int** ptr2 = &ptr;

*ptr = 10;
**ptr2 = **ptr2 + *ptr;

Step by Step

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


๐Ÿ’ก How to Think When Dealing with Pointers

Don't memorize. Follow the arrows:

ptr2
 โ†“
ptr
 โ†“
number
 โ†“
99

Each * means: "Follow one more arrow."

Example

Expression Meaning
*ptr2 Follow one arrow โ†’ ptr
**ptr2 Follow two arrows โ†’ 99

๐Ÿ“ Complete Code Example

#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;
}

Expected Output

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

๐Ÿ“Š Summary

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

Main Principle

"Follow the arrows one by one."

๐ŸŽฏ Visual Reference

Single Pointer

Memory Address    Variable Name    Value
0x1000           โ†’    ptr         โ†’  0x2000
0x2000           โ†’    number      โ†’  99

Reading *ptr:

  1. Go to ptr โ†’ get 0x2000
  2. Go to 0x2000 โ†’ get 99

Double Pointer

Memory Address    Variable Name    Value
0x3000           โ†’    ptr2        โ†’  0x1000
0x1000           โ†’    ptr         โ†’  0x2000
0x2000           โ†’    number      โ†’  99

Reading **ptr2:

  1. Go to ptr2 โ†’ get 0x1000
  2. Go to 0x1000 โ†’ get 0x2000
  3. Go to 0x2000 โ†’ get 99

โœ… Learning Status

Topic Status
Basic Pointers (*) โœ… Done
Pointer to Pointer (**) โœ… Done
Pointer to Pointer to Pointer (***) ๐Ÿš€ Next

๐Ÿ’ช Practice Exercises

Exercise 1: Basic Pointer

int x = 50;
int* p = &x;
cout << *p;  // What does this print?

Answer: 50

Exercise 2: Change Value

int x = 10;
int* p = &x;
*p = 20;
cout << x;  // What does this print?

Answer: 20

Exercise 3: Double Pointer

int x = 100;
int* p = &x;
int** pp = &p;
cout << **pp;  // What does this print?

Answer: 100


Last Updated: June 2026

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors