Example of Array of Pointer to Strings

C++
// C++ Program to illustrate how to use array of pointers to
// strings
#include <cstring>
#include <iostream>
using namespace std;

#define SIZE 5

int main()
{
    // declaring and initializing array of pointers
    char* names[SIZE]
        = { "Rahul", "Aman", "Abdul", "Ram", "Pradeep" };

    // printing the last character of each string
    for (int i = 0; i < SIZE; i++) {
        int currentStrLen = strlen(names[i]);

        // accessing character
        char lastChar = names[i][currentStrLen - 1];

        cout << lastChar << " ";
    }
    cout << endl;

    // printing whole strings
    for (int i = 0; i < SIZE; i++) {
        cout << names[i] << " ";
    }
    cout << endl;

    // updating element
    names[2] = "Fashil";

    // printing whole strings
    for (int i = 0; i < SIZE; i++) {
        cout << names[i] << " ";
    }

    return 0;
}

Output
l n l m p 
Rahul Aman Abdul Ram Pradeep 
Rahul Aman Fashil Ram Pradeep 

Array of Pointers to Strings in C++

In C++, an array is a homogeneous collection of data that is stored in a contiguous memory location. We can store almost all types of data as array elements. In this article, we will learn how to store the array of pointers to strings in C++.

Similar Reads

Array of Pointers to Strings in C++

A pointer to a string means that the pointer points to the first character in the sequence of characters that is terminated by a null character. In C++, we can use this pointer to represent strings....

Declaration of Array of Pointers to Strings

The basic form of declaring an array of pointers to strings in C++ is shown below....

Initialization of Array of Pointers to Strings

We can initialize an array of pointers to strings in C++ by using an initializer list as shown in the example below...

Accessing Elements of Array of Pointers to Strings

We can access the strings stored in this array using their index value as usual:...

Updating the Elements of Array of Pointers to Strings

The elements of array of pointers to strings are pointers themselves so we can easily change where they points to by simply assigning the new address. So, we can directly assign the new string to the index where we want to....

Example of Array of Pointer to Strings

C++ // C++ Program to illustrate how to use array of pointers to // strings #include #include using namespace std; #define SIZE 5 int main() { // declaring and initializing array of pointers char* names[SIZE] = { "Rahul", "Aman", "Abdul", "Ram", "Pradeep" }; // printing the last character of each string for (int i = 0; i < SIZE; i++) { int currentStrLen = strlen(names[i]); // accessing character char lastChar = names[i][currentStrLen - 1]; cout << lastChar << " "; } cout << endl; // printing whole strings for (int i = 0; i < SIZE; i++) { cout << names[i] << " "; } cout << endl; // updating element names[2] = "Fashil"; // printing whole strings for (int i = 0; i < SIZE; i++) { cout << names[i] << " "; } return 0; }...

Memory Representation

The advantages of using array of pointer to strings over 2D arrays of characters is that they are space efficient. Consider the following array of pointer to strings:...

Dynamic Array of Pointers to Strings in C++

All the above declarations are static and will be allocated in the heap. We can also create a dynamic array of pointers to strings using the new keyword....