The terms array and pointer are often used interchangeably. The name of an array is treated as a pointer in C++, which means that the name of an array comprises the address of an element. The array name is interpreted by C++ as the address of the first element.
For example, if we establish an array called marks to hold 20 integer values, then markings will contain the address of the first member, which is marked as marks[0]. As a result, we can say that the array name (marks) is a pointer that holds the address of the array’s first element.
Let’s understand this scenario through an example:
#include <iostream> using namespace std; int main() { int *ptr; // integer pointer declaration int marks[10]; // marks array declaration std::cout << "Enter the elements of an array :" << std::endl; for(int i=0;i<10;i++) { cin>>marks[i]; } ptr=marks; // both marks and ptr pointing to the same element.. std::cout << "The value of *ptr is :" <<*ptr<< std::endl; std::cout << "The value of *marks is :" <<*marks<<std::endl; }
Output:
Enter the elements of an array :1
2
3
4
5
6
7
8
9
10
The value of *ptr is :1
The value of *marks is :1
We declare an integer pointer and an array of integer types in the code above. The phrase ptr=marks assigns the address of marks to the ptr; this means that both the variables ‘marks’ and ‘ptr’ point to the same element, i.e., marks[0]. When we try to print the *ptr and *marks values, we get the same results. As a result, it can be demonstrated that the array name stores the address of the array’s first element.
Now discuss an array of pointers:
Array Of Pointer:
An array of pointers is an array made up of variables of the pointer type, which means the variable is a pointer to another element. Let’s say we want to make a pointer array with 5 integer pointers; the declaration would be:
int *ptr[5]; // array of 5 integer pointer.
In the above declaration, here is an array of pointers named as ptr, and it allocates 5 integer pointers in memory.
This pointer array of elements can be initialized with the address of another array of elements.
Let’s watch this case through an example:
int a; // variable declaration.
ptr[2] = &a;
In the above code, we are assigning the address of ‘a’ variable to the third element of an array ‘ptr’ pointer.
We can also retrieve the value of ‘a’ by dereferencing the pointer.
*ptr[2];
Let’s understand through an example:
#include <iostream> using namespace std; int main() { int ptr1[5]; // integer array declaration int *ptr2[5]; // integer array of pointer declaration std::cout << "Enter five numbers :" << std::endl; for(int i=0;i<5;i++) { std::cin >> ptr1[i]; } for(int i=0;i<5;i++) { ptr2[i]=&ptr1[i]; } // printing the values of ptr1 array std::cout << "The values are: " << std::endl; for(int i=0;i<5;i++) { std::cout << *ptr2[i] << std::endl; } }
Output:
Enter five numbers :
1
2
3
4
The values are: 0
1
2
3
4
In the above code, we declare an array of integer type and an array of integer pointers. We have defined the ‘for’ loop, which iterates through the elements of an array ‘ptr1’, and on each iteration, the address of element of ptr1 at index ‘i’ gets stored in the ptr2 at index ‘i’.
We’ve learned the array of pointers to an integer up to this point. We’ll now look at how to make an array of string pointers.
Array of Pointer to Strings:
An array of pointers to strings is a collection of character pointers that stores the address of a string’s initial character, also known as the base address.
The following are the distinctions between a two-dimensional array of characters and an array of pointers to strings:
- In terms of memory consumption, an array of pointers to strings is more efficient than a two-dimensional array of characters because an array of pointers to strings uses less memory to hold the strings than a two-dimensional array of characters.
- Manipulation of strings is significantly easier in a pointer array than in a two-dimensional array. Using the pointers, we can easily adjust the position of the strings.
Let’s see how to declare the array of pointers to string.
First, we declare the array of pointer to string:
char *names[5] = {"john",
"Peter",
"Marco",
"Devin",
"Ronan"};
Each element of the ‘names’ array is a string literal in this case, and each string literal holds the base address of a string’s first character. For example, names[0] contains “john’s” base address, names[1] has “Peter’s” base address, and so on. Although not all string literals are guaranteed to be stored in a contiguous memory region, the characters of a string literal are always saved in a contiguous memory area.
Let’s create a simple example“
#include <iostream> using namespace std; int main() { const char *names[5] = {"john", "Peter", "Marco", "Devin", "Ronan"}; for(int i=0;i<5;i++) { std::cout << names[i] << std::endl; } return 0; }
Output:
john
Peter
Marco
Devin
Rohan
In the above code, we have declared an array of char pointers holding 5 string literals, and the first character of each string is holding the base address of the string.