C++ Pointers

A pointer is a special type of variable that stores the memory address of another variable. This may sound difficult at first, but the basic idea is simple.

Suppose you have a variable:

int age = 20;

The value 20 is stored somewhere in the computer’s memory. A pointer can store the address of that memory location. This allows you to work with the original value indirectly.

In this tutorial, we will cover the basics of C++ Pointers, address and dereference operators, pointer arithmetic, null and void pointers, pointer to pointer, pointers with arrays and functions, dynamic memory allocation, and references.

Pointer Basics

A pointer is declared by placing * before its name.

int age = 20;
int *ptr = &age;

Here, age stores 20, while ptr stores the memory address of age.

You can think of it like this:

age → 20
ptr → address of age

The pointer does not store 20 directly. It stores where 20 is located in memory.

Address Operator

The & symbol is called the address operator. It gives you the memory address of a variable.

int age = 20;

cout << &age;

The output will be a memory address, which may look different each time you run the program.

You can store this address inside a pointer:

int *ptr = &age;

Now ptr contains the address of age.

Dereference Operator

The * operator is also used to access the value stored at the address held by a pointer. In this situation, it is called the dereference operator.

int age = 20;
int *ptr = &age;

cout << *ptr;

Output

20

You can also change the original value through the pointer:

*ptr = 25;

Now age is also 25.

Pointer Arithmetic in C++

Pointers can be increased or decreased to move through memory locations. This is called Pointer Arithmetic in C++.

It is commonly used when working with arrays.

int numbers[3] = {10, 20, 30};

int *ptr = numbers;

cout << *ptr << endl;

ptr++;

cout << *ptr;

Output

10
20

When ptr++ is used, the pointer moves to the next element of the array.

Null Pointer

A null pointer is a pointer that does not point to a valid memory location.

You can create one using nullptr.

int *ptr = nullptr;

nullptr is safer and clearer than using 0 or NULL for a pointer.

You should not dereference a null pointer because it does not point to a valid object.

Void Pointer

A void pointer can store the address of a value of any data type.

int age = 20;

void *ptr = &age;

However, you cannot directly dereference a void*. You first need to convert it to the appropriate pointer type.

Void pointers are mostly useful in situations where the data type is not known beforehand.

Pointer to Pointer

A pointer can also store the address of another pointer. This is called a pointer to pointer.

int age = 20;
int *ptr = &age;
int **ptr2 = &ptr;

Here, ptr2 stores the address of ptr.

You can access the original value using:

cout << **ptr2;

Output

20

Pointer and Arrays

The name of an array can be used as a pointer to its first element.

int numbers[3] = {10, 20, 30};

int *ptr = numbers;

cout << *ptr;

Output

10

You can then move through the array using pointer arithmetic.

Pointers with Functions

Pointers can be passed to functions when you want the function to work with the original variable.

void change(int *ptr)
{
    *ptr = 50;
}

int number = 10;

change(&number);

cout << number;

Output

50

The function changes number because it receives its address.

Dynamic Memory Allocation in C++

Normally, memory for a variable is managed automatically. Sometimes, you need to request memory while the program is running. This is called Dynamic Memory Allocation in C++.

The new operator is used to allocate memory.

int *ptr = new int;

*ptr = 25;

cout << *ptr;

delete ptr;

Here, new creates memory for an integer, while delete releases that memory when it is no longer needed.

For an array:

int *numbers = new int[5];

delete[] numbers;

Remember to release dynamically allocated memory to avoid memory leaks.

C++ References

A reference is another name for an existing variable. It is created using &.

int age = 20;
int &ref = age;

ref = 25;

cout << age;

Output

25

Here, ref and age refer to the same value. References are often used with functions when you want to work with the original variable without using pointer syntax.


Frequently Asked Questions (FAQs)

Q1. What is a pointer in C++?

A pointer in C++ is a variable that stores the memory address of another variable. Pointers are commonly used for direct memory access, arrays, functions, and dynamic memory management.

Q2. How do I access the address of a variable using a pointer in C++?

Use the address operator (&) to get a variable’s memory address and store it in a pointer. The pointer can then be used to access that address.

Q3. What happens when a pointer is dereferenced in C++?

Dereferencing a pointer using * accesses the value stored at the memory address held by that pointer.

Q4. When should dynamic memory allocation be used in C++?

Use Dynamic Memory Allocation in C++ when memory needs to be created during program execution or when the required amount of memory cannot be determined beforehand.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top