Detailed Explanation of Pointer Arrays in C++

Arrays and pointers are closely related in C++. In C++, the name of an array is treated as a pointer, meaning the name of the array contains the address of the first element. C++ treats the array name as pointing to the address of the first element of the array. For example, if we create an array marks that contains 20 integer values, then marks will contain the address of the first element marks[0]. Therefore, we can say that the array name (marks) is a pointer to the first element of the array.

Let’s understand this with an example.

#include <iostream>
using namespace std;
int main() {
   int *ptr;  // Integer pointer declaration
   int marks[10]; // Integer array declaration
   std::cout << "Input the elements of the array:" << std::endl;
   for(int i=0; i<10; i++) {
       cin >> marks[i];
   }
   ptr = marks; // marks and ptr point to the same element
   std::cout << "*ptr's value is:" << *ptr << std::endl;
   std::cout << "*marks's value is:" << *marks << std::endl;
}

In the code above, we declared an integer pointer and an integer array. We used the statement ptr=marks to assign the address of marks to ptr, which means that both variables ‘marks’ and ‘ptr’ point to the same element, which is marks[0]. When we try to print the values of ptr and marks, their values are the same. Thus, it proves that the array name stores the address of the first element of the array.

Output:

Detailed Explanation of Pointer Arrays in C++

πŸ‘‡ Click to Claim πŸ‘‡
πŸ‘‰ Collection of C Language Knowledge Materials

Pointer Arrays

A pointer array is an array consisting of pointer-type variables, which means the variables are pointers that point to some other elements. Suppose we create a pointer array containing 5 integer pointers, its declaration is as follows:

int *ptr[5];         // Array of 5 integer pointers

In the above declaration, we declared a pointer array named ptr and allocated space for 5 integer pointers in memory.

Elements of the pointer array can also be initialized by assigning the addresses of other elements. Let’s observe this with an example.

int a; // Variable declaration
ptr[2] = &a;

In the code above, we assigned the address of variable a to the third element of the pointer array ptr[2].

We can also retrieve the value of a by dereferencing the pointer.

*ptr[2];

Let’s understand this with an example.

#include <iostream>
using namespace std;
int main() {
   int ptr1[5]; // Integer array declaration
   int *ptr2[5]; // Integer pointer array declaration
   std::cout << "Input 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];
   }
   // Print the values of ptr1 array
   std::cout << "These values are:" << std::endl;
   for(int i=0; i<5; i++) {
       std::cout << *ptr2[i] << std::endl;
   }
}

In the code above, we declared an integer array and an integer pointer array. We defined a ‘for’ loop to iterate through the elements of the ptr1 array, storing the address of the ith element of ptr1 in the ith position of ptr2 during each iteration.

Output:

Detailed Explanation of Pointer Arrays in C++

So far, we have learned about integer pointer arrays. Now, we will see how to create a pointer array that points to strings.

Pointer Arrays Pointing to Strings

A pointer array pointing to strings is an array of character pointers that holds the address of the first character of the string, also known as the base address of the string.

The differences between a pointer array pointing to strings and a two-dimensional character array are as follows:

  • Compared to two-dimensional character arrays, pointer arrays pointing to strings are more memory-efficient because they consume less memory than two-dimensional character arrays.

  • In pointer arrays, manipulating strings is easier than in 2D arrays. We can also easily change the position of strings using pointers.

Let’s see how to declare a pointer array pointing to strings.

First, we declare a pointer array pointing to strings:

char *names[5] = {"john",                   "Peter",                   "Marco",                   "Devin",                   "Ronan"};

In the code above, we declared a pointer array names of size 5. In this case, we initialized it during the declaration, so we do not need to provide the size of the pointer array. The above code can also be rewritten as:

char *names[] = {"john",                   "Peter",                   "Marco",                   "Devin",                   "Ronan"};

In the above case, each element of the names array is a string literal, each of which holds the base address of the first character of the string. For example, names[0] contains the base address of the string “john”, names[1] contains the base address of the string “Peter”, and so on. It cannot be guaranteed that all string literals are stored in contiguous memory locations, but the characters of string literals are stored in contiguous memory locations.

Let’s create a simple example.

#include <iostream>
using namespace std;
int main() {
   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 code above, we declared a char pointer array containing 5 string literals, each of which saves the base address of the first character of the string.

Output:

Detailed Explanation of Pointer Arrays in C++

Detailed Explanation of Pointer Arrays in C++


 Popular Recommendations
  • CLion Tutorial – Features in CLion

  • C Language Algorithm – “Different Paths” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of the sizeof() Operator in C++

Leave a Comment