A pointer is a variable that stores the memory address of another variable.
Normally, when you create a variable in C++, it stores data directly.
A pointer, however, stores the address where that data lives in memory.
#include <iostream>
using namespace std;
int main() {
int age = 25; // A normal integer variable
int* ptr = &age; // Pointer that stores the address of 'age'
cout << "Value of age: " << age << endl;
cout << "Address of age (&age): " << &age << endl;
cout << "Pointer value (ptr): " << ptr << endl;
cout << "Value pointed to (*ptr): " << *ptr << endl;
return 0;
} Value of age: 25
Address of age (&age): 0x61ff08
Pointer value (ptr): 0x61ff08
Value pointed to (*ptr): 25 | Operator | Description | Example |
|---|---|---|
& | “Address-of” operator – gives the memory address of a variable | ptr = &x; |
* | “Dereference” operator – gives the value stored at that address | cout << *ptr; |
int* ptr; // pointer to int
float* fptr; // pointer to float
char* cptr; // pointer to char A pointer type must match the type of variable it points to (you can’t store the address of a float in an int*).
int num = 10;
int* ptr = #
cout << *ptr; // prints 10
*ptr = 20; // changes the value of num
cout << num; // prints 20 Here, modifying *ptr also changes num because they refer to the same memory location.
In C++, the name of an array acts like a pointer to its first element.
int arr[3] = {10, 20, 30};
int* ptr = arr; // same as &arr[0]
cout << *ptr << endl; // 10
cout << *(ptr + 1) << endl; // 20
cout << *(ptr + 2) << endl; // 30 You can move through arrays using pointer arithmetic.
| Operation | Meaning |
|---|---|
ptr + 1 | Moves to the next element |
ptr - 1 | Moves to the previous element |
ptr++ / ptr-- | Same as above |
int nums[3] = {1, 2, 3};
int* p = nums;
cout << *p << endl; // 1
p++;
cout << *p << endl; // 2 You can pass pointers to functions to modify variables directly.
#include <iostream>
using namespace std;
void changeValue(int* ptr) {
*ptr = 50;
}
int main() {
int num = 10;
changeValue(&num);
cout << num; // prints 50
} Here, the function modifies the original num using its address.
A pointer can also store the address of another pointer.
int num = 100;
int* ptr = #
int** pptr = &ptr;
cout << **pptr; // prints 100 C++ allows creating variables at runtime using new and deleting them using delete.
int* p = new int; // allocate memory
*p = 10;
cout << *p; // prints 10
delete p; // free memory int* arr = new int[3];
arr[0] = 5; arr[1] = 10; arr[2] = 15;
for (int i = 0; i < 3; i++)
cout << arr[i] << " ";
delete[] arr; A pointer that doesn’t point to any valid memory address.
int* ptr = nullptr;
if (ptr == nullptr)
cout << "Pointer is empty!"; Always initialize pointers — uninitialized pointers can cause undefined behavior or crashes.
✅ Always initialize pointers (nullptr if not assigned).
✅ Use delete for every new to avoid memory leaks.
✅ Prefer smart pointers (std::unique_ptr, std::shared_ptr) in modern C++.
✅ Avoid pointer arithmetic unless necessary.
✅ Use references when ownership or nullability is not required.
| Concept | Example |
|---|---|
| Declare pointer | int* ptr; |
| Assign address | ptr = &x; |
| Dereference pointer | *ptr |
| Pointer to pointer | int** pptr; |
| Dynamic memory | int* p = new int; delete p; |
| Null pointer | int* ptr = nullptr; |
Latest tech news and coding tips.
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…
1. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…