What is a Pointer?
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.

Example:
#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;
}
Output (example)
Value of age: 25
Address of age (&age): 0x61ff08
Pointer value (ptr): 0x61ff08
Value pointed to (*ptr): 25
Key Pointer Operators
| 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; |
Declaring Pointers
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*).
Pointer Assignment and Dereferencing
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.
Pointers and Arrays
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.
Pointer Arithmetic
| Operation | Meaning |
|---|---|
ptr + 1 | Moves to the next element |
ptr - 1 | Moves to the previous element |
ptr++ / ptr-- | Same as above |
Example:
int nums[3] = {1, 2, 3};
int* p = nums;
cout << *p << endl; // 1
p++;
cout << *p << endl; // 2
Pointers and Functions
You can pass pointers to functions to modify variables directly.
Example: Call by Reference using Pointers
#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.
Pointer to Pointer (Double Pointer)
A pointer can also store the address of another pointer.
int num = 100;
int* ptr = #
int** pptr = &ptr;
cout << **pptr; // prints 100
Dynamic Memory Allocation
C++ allows creating variables at runtime using new and deleting them using delete.
Example:
int* p = new int; // allocate memory
*p = 10;
cout << *p; // prints 10
delete p; // free memory
Dynamic Array Example:
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;
Null Pointer
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.
Best Practices
✅ 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.
Summary Table
| 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.