How to Create an Array of Pointers in C++

Hey guys,
Today we will be learning how to create an array of pointers in C++
So read on…

What is a pointer?

A pointer is nothing but a value stored in a variable that points to another variable’s address.
Using a pointer makes the program faster.

Assume I have a variable ‘a’ which has the value 10 stored in it.
I have another variable b which is a pointer and points to the value of a.
Hence the address of a is stored in b

So if the address of a is 1000,
Data inside b would be 1000

What is an array?

Array is a collection of homogenous data stored linearly in a sequential format.
Each variable inside an array is given a unique address and is called via that index.

Since an array is basically a collection of many elements, we can create a pointer that points to every element.

But an array is mentioned by a variable that points to the first address or the base address.
So to assign pointers to the array, we need to create an array of pointers.

The syntax is exactly the same as the referencing and dereferencing of a pointer.

Syntax:

<datatype> *<variable>[<size>];       //creates an array of pointers.
<variable>[<index>] = &<array_variable>[<index>];  //assigning values.

Example:

int arr[10];      //array
int *ptr[10];     //pointer array
ptr[2] = &arr[2]; //assigning values

Create an Array of Pointers in C++

Take a look at the following code:

#include <iostream>
using namespace std;
int main()
{
    int arr[3] = {};
    int *ptr[3];
    
    cout<<"Enter 3 values\n";
    
    for(int i=0;i<3;i++)
    {
        cin>>arr[i];
        ptr[i] = &arr[i];
    }
    
    for(int i=0;i<3;i++)
    {
        cout<<"Address of "<<*ptr[i]<<" is "<<ptr[i]<<"\n";
    }
}

Let us take a look at it line by line:

  1. int arr[3] = {};
    int *ptr[3];

    First, we are creating an array of size 3 called arr that will store 3 integer type values.
    We are also creating a pointer type of array of the same size using the above syntax.

  2. for(int i=0;i<3;i++)
        {
            cin>>arr[i];
            ptr[i] = &arr[i];
        }

    Here we are accepting the 3 elements from the user and storing them in arr.
    We are also storing the addresses of those 3 elements in ptr with the same index location as that of arr.

  3. for(int i=0;i<3;i++)
        {
            cout<<"Address of "<<*ptr[i]<<" is "<<ptr[i]<<"\n";
        }

    Now finally we are printing the values as well as the address.
    When we print using *, we are printing the values the address at a particular index location points to.
    The array of pointers is filled with addresses so when we simply print the array without the *, we are printing the address of the element it is associated with.

If you have followed me up till here, your output should look something like this:

5
10
20
Address of 5 is 0x7ffdbab47fc4
Address of 10 is 0x7ffdbab47fc8
Address of 20 is 0x7ffdbab47fcc

So we are printing the value as well as the address it is stored in.

This brings us to the end of this tutorial.
If you have any doubts, feel free to ask it in the comment section. Thanks

Leave a Reply

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