Arrays and pointers share a strong connection. Within C++, an array's name functions akin to a pointer, signifying that the array's name encompasses the element's address. In C++, the array name signifies the first element's address. For instance, upon creating an array like 'marks' to store 20 integer values, 'marks' will hold the address of the initial element, 'marks[0]'. Hence, it can be affirmed that the array name ('marks') acts as a pointer that retains the primary element's address within the array.
Let's explore this situation using an illustration.
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;
}
In the provided code snippet, we define an integer pointer and an integer array. By setting the pointer to point to the address of the array using the assignment ptr=marks;, both 'marks' and 'ptr' variables now reference the same element, specifically marks[0]. Consequently, when we display the values stored at ptr and marks, they are identical. This demonstrates that the array name holds the memory address of the initial array element.
Output
Array of Pointers
A pointer array is an array comprising variables of pointer type, where each variable acts as a reference pointing to another element in memory. For instance, if we were to establish an array of pointer containing 5 integer pointers, the declaration would resemble the following:
int *ptr[5]; // array of 5 integer pointer.
In the declaration provided above, we define an array of pointers called ptr, which reserves memory for 5 integer pointers.
The element of an array of a pointer can alternatively be set by assigning the memory address of another element. Let's explore this scenario using an illustration.
int a; // variable declaration.
ptr[2] = &a;
In the provided code snippet, we are assigning the memory location of variable 'a' to the third element of an array named 'ptr'.
We can also access the value of 'a' by dereferencing the pointer.
*ptr[2];
Let's understand through an example.
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;
}
}
In the previous code snippet, we initialize an array of integers and an array of integer pointers. A 'for' loop is specified to go through the elements of an array named 'ptr1'. During each iteration, the memory address of the element in 'ptr1' at position 'i' is stored in 'ptr2' at the same index 'i'.
Output
Till now, we have learnt the array of pointers to an integer. Now, we will see how to create the array of pointers to strings.
Array of Pointer to Strings
An array of string pointers is a collection of pointers to characters, storing the memory location of the initial character within a string, also known as the string's base address.
The variances between an array of string pointers and a two-dimensional array of characters are as follows:
- An array of string pointers is superior in memory efficiency compared to a 2D array of characters as it requires less memory to store the strings.
- When working with an array of pointers, string manipulation is simpler than with a 2D array. Shifting the strings' positions using pointers is also more straightforward.
Let's explore how to define an array of string pointers.
First, we declare the array of pointer to string:
char *names[5] = {"john",
"Peter",
"Marco",
"Devin",
"Ronan"};
In the preceding code, we defined a pointer array named 'names' with a capacity of 5 elements. Since the initialization was performed during declaration, specifying the size of the pointer array is unnecessary. The code snippet above could be rephrased as:
char *names[ ] = {"john",
"Peter",
"Marco",
"Devin",
"Ronan"};
In this scenario, every item in the 'names' array is a string constant, with each string constant representing the starting memory address of a string. For instance, names[0] stores the memory address of "john", names[1] holds the memory address of "Peter", and so forth. While it's not assured that all string constants will be positioned consecutively in memory, the characters within each string constant are indeed stored contiguously.
Let's create a simple example.
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;
}
In the provided code snippet, we've initialized an array of character pointers containing 5 string constants, where the initial character of each string stores the starting memory address of that string.
Output
john
Peter
Marco
Devin
Ronan