Pointers in C++ – Complete Tutorial
Last Updated on: 17th Dec 2025 18:43:22 PM
Introduction
Pointers are one of the most powerful and important features of C++.
They allow a program to directly access and manipulate memory, making C++ efficient and flexible.
Understanding pointers is essential for:
-
Dynamic memory allocation
-
Arrays and strings
-
Function call optimization
-
Data structures like linked lists, trees, and graphs
Definition of Pointer
A pointer is a variable that stores the memory address of another variable.
Instead of storing a value directly, a pointer stores where the value is located in memory.
Real-life example (Basic Idea)
Think of a house and its address:
-
House → actual value
-
Address → pointer
The address tells you where the house exists.
1. Declaring and Initializing a Pointer
Concept Explanation
To use a pointer, we must:
-
Declare it
-
Store the address of a variable using the address-of operator (&)
Example
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr;
ptr = #
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value stored in ptr: " << ptr << endl;
return 0;
}
Output
Value of num: 10
Address of num: 0x61ff08
Value stored in ptr: 0x61ff08
Short Explanation of Output
The pointer stores the memory address of num, not the value itself.
The address printed by &num and ptr is the same.
2. Dereferencing a Pointer
Concept Explanation
Dereferencing means accessing the value stored at the memory address pointed to by a pointer.
This is done using the * operator.
Real-life example
Using a home address to reach the house and see what is inside.
Example
#include <iostream>
using namespace std;
int main() {
int num = 25;
int *ptr = #
cout << "Value using variable: " << num << endl;
cout << "Value using pointer: " << *ptr << endl;
return 0;
}
Output
Value using variable: 25
Value using pointer: 25
Short Explanation of Output
Dereferencing the pointer gives the actual value stored at that memory location.
3. Pointer and Variable Relationship
Concept Explanation
Changing the value using a pointer will also change the original variable, since both refer to the same memory location.
Real-life example
Two keys opening the same locker — changes made by one key are visible to the other.
Complete Program
#include <iostream>
using namespace std;
int main() {
int num = 50;
int *ptr = #
*ptr = 100;
cout << "Value of num: " << num << endl;
return 0;
}
Output
Value of num: 100
Short Explanation of Output
Updating the value using the pointer updates the original variable.
4. Null Pointer
Concept Explanation
A null pointer does not point to any memory location.
It is used to avoid accidental access to invalid memory.
Real-life example
A phone contact with no number saved.
Example
#include <iostream>
using namespace std;
int main() {
int *ptr = NULL;
if (ptr == NULL) {
cout << "Pointer is null" << endl;
}
return 0;
}
Output
Pointer is null
Short Explanation of Output
The program safely checks that the pointer does not point to any valid memory.
5. Pointer to Pointer
Concept Explanation
A pointer can also store the address of another pointer.
This is called a pointer to pointer.
Real-life example
-
Home → address
-
Address book → address of address
Example
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr = #
int **pptr = &ptr;
cout << "Value of num: " << num << endl;
cout << "Value using ptr: " << *ptr << endl;
cout << "Value using pptr: " << **pptr << endl;
return 0;
}
Output
Value of num: 10
Value using ptr: 10
Value using pptr: 10
Short Explanation of Output
The double pointer accesses the value by dereferencing twice.
6. Pointers and Arrays
Concept Explanation
Array name itself acts as a pointer to the first element of the array.
Real-life example
A row of lockers, where the first locker number gives access to the entire row.
Example
#include <iostream>
using namespace std;
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr;
for (int i = 0; i < 3; i++) {
cout << *(ptr + i) << " ";
}
return 0;
}
Output
10 20 30
Short Explanation of Output
Pointer arithmetic allows accessing array elements using memory offsets.
7. Pointers in Function (Call by Reference)
Concept Explanation
Pointers allow functions to modify actual arguments, not just copies.
Real-life example
Giving someone the original key, not a duplicate.
Complete Program
#include <iostream>
using namespace std;
void update(int *x) {
*x = 200;
}
int main() {
int num = 50;
update(&num);
cout << "Updated value: " << num << endl;
return 0;
}
Output
Updated value: 200
Short Explanation of Output
The function modifies the original variable using its memory address.
8. Dynamic Memory Allocation (new and delete)
Concept Explanation
Pointers are used to allocate memory dynamically during runtime using new and release it using delete .
Real-life example
Renting a hotel room only when needed and vacating it after use.
Complete Program
#include <iostream>
using namespace std;
int main() {
int *ptr = new int;
*ptr = 75;
cout << "Value: " << *ptr << endl;
delete ptr;
return 0;
}
Output
Value: 75
Short Explanation of Output
Memory is allocated at runtime and freed after use to avoid memory leaks.
Advantages of Pointers
-
Efficient memory usage
-
Dynamic memory allocation
-
Faster program execution
-
Essential for data structures
Disadvantages of Pointers
-
Complex syntax
-
Risk of memory leaks
-
Dangling pointers
-
Difficult debugging
Summary
-
Pointers store memory addresses
-
Dereferencing accesses actual values
-
Pointers enable call by reference
-
Arrays and pointers are closely related
-
Dynamic memory allocation uses pointers
Keep practicing — you're doing amazing!
Happy Coding! ![]()