C++ Array of Strings
In this page:
C-style Array of Strings
A C-style array of strings can be declared as a 2D character array, like char names[3][10];, where the first dimension counts how many strings you have and the second caps how many characters (including the terminator) each individual string can hold. This rigid per-string size limit is exactly what modern C++ tries to avoid.
Example: C-style Array of Strings
#include <iostream>
int main() {
char names[3][10] = {"Alice", "Bob", "Carol"};
std::cout << names[0] << " " << names[1] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
C++ std::string Array
The modern approach declares an ordinary array of std::string objects, like string names[3];, where each element manages its own memory and can grow to whatever length it needs — there's no fixed character limit to worry about per string, unlike the C-style 2D array approach.
Example: C++ std::string Array
#include <iostream>
#include <string>
int main() {
std::string names[3];
names[0] = "Alice";
std::cout << names[0] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing String Arrays
You can populate a string array right at declaration using an initializer list, like string days[] = {"Mon", "Tue", "Wed"};, and just as with numeric arrays, the compiler will automatically count the elements to size the array if you leave the brackets empty.
Example: Initializing String Arrays
#include <iostream>
#include <string>
int main() {
std::string days[] = {"Mon", "Tue", "Wed"};
std::cout << days[1] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing and Modifying Strings
Individual strings inside the array are accessed with a single index, like names[1], and since each element is a full std::string, you can immediately call any string method on it, like names[1].append("son");, to modify that specific entry in place.
Example: Accessing and Modifying Strings
#include <iostream>
#include <string>
int main() {
std::string names[] = {"Alice", "Bob"};
names[1] = "Bobby";
std::cout << names[1] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterating Through String Arrays
A regular indexed for loop or a range-based for loop can both walk through a string array — the indexed version is useful when you also need to know each string's position, while the range-based version is cleaner when you only care about the string values themselves.
Example: Iterating Through String Arrays
#include <iostream>
#include <string>
int main() {
std::string days[] = {"Mon", "Tue", "Wed"};
for (const std::string &day : days) {
std::cout << day << " ";
}
std::cout << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings