Understanding C++ String Array With Examples

Hey, Folks! So, as programmers, we often deal with Arrays of all data types. We’ll cover C++ string array in today’s article.

Ways to declare an C++ String Array

Ways To Create C++ String Array

1. The String keyword to Create String Array in C++

C++ provides us with ‘string‘ keyword to declare and manipulate data in a String array.

The string keyword allocates memory to the elements of the array at dynamic or run-time accordingly. Thus it saves the headache of static memory allocation of data-elements.

Syntax: To declare an array of string using ‘string’ keyword

Further, we can initialize the array of string using the below syntax:

Example 1:

In the above example, we have initialized the string array and have used C++ for loops to traverse through the array and print the data items present in the string array.


Example 2:

As you all can witness, in the above example, we did accept the data items of the string array from the console i.e. user input is taken and we have printed the elements of the string array.


2. Using C++ STL Container – Vector

C++ Standard Template Library provides us with containers to work with data and store it efficiently.

Vector, being one such container, stores the array elements in a dynamic manner. Thus, C++ Vectors can be used to create a string array and manipulate the same easily.


  • The vector.push_back(element) method is used to add elements to the vector string array.
  • The vector.size() method is used to calculate the length of the array i.e. the count of the elements input to the string array.



3. Using 2D char array

A 2D array represents an array of string in C++. So, we can use a 2D char array to represent string type elements in an array.

The char array creates and stores elements at static or compile-time i.e. the number and size of elements stay fixed/constant.



In the above snippet of code, we have created a char array to store string type elements. i.e. char array[5][10]. Here 5 depicts the count of string elements and 10 points to the maximum size of the input string.


C++ String Array as an Argument to a Function

A string array can also be passed to a function as an argument the same way as another non-string type array is passed to it.





In this article, we have understood ways to create a string arrays and techniques to use it in a function.


By admin

Leave a Reply

%d bloggers like this: