What is a Pointer?

pointer is a variable that stores the memory address of another variable.

Download our iOS app

Normally, when you create a variable in C++, it stores data directly.
A pointer, however, stores the address where that data lives in memory.

Start learning programming

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

OperatorDescriptionExample
&“Address-of” operator – gives the memory address of a variableptr = &x;
*“Dereference” operator – gives the value stored at that addresscout << *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 = &num;

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

OperationMeaning
ptr + 1Moves to the next element
ptr - 1Moves 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 = &num;
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_ptrstd::shared_ptr) in modern C++.
✅ Avoid pointer arithmetic unless necessary.
✅ Use references when ownership or nullability is not required.

Summary Table

ConceptExample
Declare pointerint* ptr;
Assign addressptr = &x;
Dereference pointer*ptr
Pointer to pointerint** pptr;
Dynamic memoryint* p = new int; delete p;
Null pointerint* ptr = nullptr;

Recent Posts

JavaScript Memoization

In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…

1 week ago

CSS Container Queries: Responsive Design That Actually Makes Sense

For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…

1 week ago

Cron Jobs & Task Scheduling

1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…

1 week ago

Differences Between a Website and a Web App

Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…

3 weeks ago

Essential VS Code Extensions Every Developer Should Use

Visual Studio Code (VS Code) is powerful out of the box, but its real strength…

4 weeks ago

JavaScript Variables

1. What Is a Variable in JavaScript? A variable is a named container used to store data…

4 weeks ago