Create an Array of pointers in C++

This tutorial will discuss how to create an array of pointers in C++.

When declaring an array, we need to provide two pieces of information. The first is the data type of the elements in the array, and the second is the number of elements. To create an array of pointers, we specify the data type as a pointer type. For instance, if we want to create an array of integer pointers, the data type will be int*.

int *arr[len];

In the above line, we declared an array of 5 integer pointers. However, we haven’t initialized the array with any value, so currently, all the pointers in the array point to garbage locations.

Now, we can iterate over the array and initialize each integer pointer with a proper memory location. To do this, we allocate a new integer on the heap and assign its address to the pointer, as shown here.

// Allocate memory for each element of the array
for (int i = 0; i < len; ++i)
{
    // Dynamically allocate memory for each integer pointer
    arr[i] = new int;

    // Assign a value to each integer through the pointer
    *arr[i] = i * 10;
}

After this step, we have initialized our array of integer pointers with 5 different integers on the heap, and each value is accessed using the integer pointer in the array. With the array of integer pointers ready, we can iterate over each pointer in the array. For every pointer, we access the data at the memory location pointed to by that pointer and print it on the console.

for (int i = 0; i < len; ++i)
{
    std::cout << "arr[" << i << "] = "
                << *arr[i]
                << std::endl;
}

However, suppose we no longer need the integer array. In that case, it’s crucial to deallocate the memory allocated on the heap that was used to initialize our array of pointers. To achieve this, we iterate over each integer pointer in the array and free the memory allocated to it using the delete operator.

// Deallocate memory for each element of the array
for (int i = 0; i < len; ++i)
{
    // Free memory for each integer pointer
    delete arr[i];
}

It’s essential to remember that if you’re using an array of pointers to store addresses of memory allocated on the heap, you must delete that memory to avoid memory leaks.

Let’s see the complete example,

#include <iostream>

int main()
{
    const size_t len = 5;

    // Create an array of pointers to integers
    int *arr[len];

    // Allocate memory for each element of the array
    for (int i = 0; i < len; ++i)
    {
        // Dynamically allocate memory for each integer pointer
        arr[i] = new int;

        // Assign a value to each integer through the pointer
        *arr[i] = i * 10;
    }

    // Access and print values through the pointers
    for (int i = 0; i < len; ++i)
    {
        std::cout << "arr[" << i << "] = "
                  << *arr[i]
                  << std::endl;
    }

    // Deallocate memory for each element of the array
    for (int i = 0; i < len; ++i)
    {
        // Free memory for each integer pointer
        delete arr[i];
    }

    return 0;
}

Output

arr[0] = 0
arr[1] = 10
arr[2] = 20
arr[3] = 30
arr[4] = 40

Summary

We learned how to declare and initialize an Array of pointers in C++.

Summary

Today, we learned how to create an array of pointers in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top