Updated April 18, 2023
Introduction to C++ array of pointers
The array is something that holds the list of elements, and pointers are something that holds the address of the variable. So an array of pointers represents an array that holds the address of the elements which are present inside the array. this array of pointers is required when we want to manipulate our array data. The array of pointers makes the manipulation easy for us because the array is very bound in nature, so this array of the pointer will contain the address of each element present in the array. The array of pointers hold the memory address of the array elements. We will discuss this in more detail in the coming section. In this topic, we are going to learn about the C++ array of pointers.
Syntax
In c++, if we want to declare an array of the pointer, then we have to create an array that will hold the address of the other elements, which point to some value for that address. For better understanding, we will see its syntax how to use this while programming see below;
type *name_of_pointer [size_or_array];
In the above syntax, if we want to create an array of pointers, then we have to first define the type of the array pointer; after that, we define the name of our pointer but remember always we define a pointer using the ‘*’ astrict symbol in c++, immediately after this we define the size of the array which means how many elements it is going to hold. For better understating we will see one practice syntax see below;
Example:
int *myptr[10];
This can define an array of pointers in C++; after that, we can assign them the address of the variable from the array.
How the array of pointers work in C++?
As of now, we know that array of pointers is used to store the address of the array elements. In short, this pointer value points to the address of the elements present inside the array; if we want to access them, we can access each of the elements of the array by using their address because it is pointing to them only. So we can say these are the pointer pointing to some other value of the element in C++. By the use of this, it makes our operations fast, increase the performance as well. Also, the manipulation of the array becomes easy because now we have the address of the element with us, which makes the access to the element. So now we will see how they work in c++ programming language.
First, we will see how we can define an array of pointers see below;
int *myptr[10];
In the above line of code, we are declaring an array of pointers which can hold 10 elements for us. But this array will hold the address of the elements. To get the address of the element, we have the ‘&’ keyword in c++; by the use of this, we can get the address of the element. The address is nothing but the location of the element where it is stored in the memory. Always address is referred to as the memory address of the element. This memory address will turn point to the element which is stored at this location.
Suppose we have some real-time case where we need to modify the array, but they are fixed in nature, so that manipulation will be very costly for us. So we can use or maintain an array of pointers which will reduce our effort while modifying the array elements. Now we will see how they work internally;
Suppose we have an array with 5 elements inside it;
Example:
int myarray [5] = {20, 40, 60, 80 , 100};
int *myptr [5]; // this holds the address of the elements of the array.
As know we can see in the above image, we have 5 elements inside the array; also, we have some memory address for each of the elements in the array. Let’s say it is memory-add1, memory-add2, and so on for understanding purpose. So what will an array of pointers will do? They will store the address of the elements and refer them to that address only in the future, so this is how it works internally in c++.
How to create an array of pointers in C++?
Below are the steps to create an array of pointers in c++, which are as follows;
1. First, we need to create an array that contains some elements. Let’s say 10 elements for now.
Example:
int myarray [2] = {100, 200};
2. After this, we have to create an array of pointers that will store the address of the array elements.
Example:
int *myptr[2];
3. To get the address of the array elements, we have the ‘&’ operator in c++ this will give us the address of the element in memory.
Example:
myptr[1] = &myarray[1]; // so on ..
4. Then, we will store this address of the elements into the array of pointers by iterating them using for loop. This depends on the logic of how we want them to be loaded in an array.
5. Now, we can access the elements of the array using the array of pointers; it will give us the same result.
In the next section, we will see one example of how to implements this in c++.
Example of C++ array of pointers
In this example, we create an array that contains multiple elements and tries to access the array by using an array of pointers and getting their values.
Code:
#include <iostream>
int main() {
std::cout << "Demo to show array of pointers !!!\n";
int myarray[5] = {100, 200, 300, 400, 500};
int *myptr[5];
std::cout << "Value of array !!\n";
for (int i = 0; i < 5; i+) {
std::cout << myarray[i] << std::endl;
}
myptr[0] = &myarray[0];
myptr[1] = &myarray[1];
myptr[2] = &myarray[2];
myptr[3] = &myarray[3];
myptr[4] = &myarray[4];
std::cout << "Value of array of pointers !!\n";
for (int i = 0; i < 5; i++) {
std::cout << *myptr[i] << std::endl;
}
}
Output:
Conclusion
By using an array of pointers, we can easily manipulate our array because they are very bound in nature. We just need to access the elements by using the address of the elements. This address is nothing but the location of the element from the memory to directly access them.
Recommended Articles
This is a guide to the C++ array of pointers. Here we discuss How the array of pointers work and how to create it in C++, and the example. You can also look at the following article to learn more –