Support Online
Skip to main content

Creating a C++ String Array

What Will You Learn in This Guide?

In this guide, you'll learn three effective ways to create string arrays (text arrays) in C++.
You'll start with the modern use of std::string, then explore the dynamic advantages of std::vector, and finally examine the traditional char[][] method.
You will also see how to pass these arrays as parameters to functions.

Modern C++: Std::string Array Usage

The simplest and safest method in C++ is to define a fixed-size array of type std::string.
This method has a fixed size at compile time.

#include <iostream>
#include <string>
using namespace std;

int main() {
string hizmetler[4] = {"Web Sunucu", "Veritabani", "Yedekleme", "Guvenlik Duvari"};

cout << "GenixNode Hizmetleri:\n";
for (int i = 0; i < 4; i++)
cout << hizmetler[i] << "\n";
}

This example shows defining a fixed size std::string array and printing its elements.


The Most Flexible Method: Using std::vectorstd::string

The std::vector class creates arrays that can grow dynamically. It is the most modern and recommended approach for text arrays in C++.


#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
vector<string> bolgeler;

bolgeler.push_back("tr-istanbul-1");
bolgeler.push_back("eu-frankfurt-2");
bolgeler.push_back("us-nyc-3");

int boyut = bolgeler.size();

cout << "Bolge Kodlari (" << boyut << " adet):\n";
for (int i = 0; i < boyut; i++)
cout << bolgeler[i] << "\n";
}
  • This example shows adding elements in dynamic arrays using std::vector<string>.

Old Style: Using 2D char Array

This method is inherited from the C language. Both the number of elements and the length of each text must be constant.


#include <iostream>
#include <cstring>
using namespace std;

int main() {
char musteri_isimleri[5][10] = {"Ali", "Can", "Ece", "Deniz", "Furkan"};

cout << "Musteri Listesi:\n";
for (int i = 0; i < 5; i++)
cout << musteri_isimleri[i] << "\n";
}

char[5][10] holds 5 texts and allocates a maximum of 10 characters for each text. This method is static.


Passing Text Strings to Functions

C++ arrays can be passed as references to functions. However, since the array becomes a pointer, size information must be passed separately.


#include <iostream>
#include <string>
using namespace std;

void listele(string arr[], int boyut) {
for (int i = 0; i < boyut; i++)
cout << arr[i] << endl;
}

int main() {
string sunucular[3] = {"tr1-node01", "tr1-node02", "tr1-node03"};
int boyut = sizeof(sunucular) / sizeof(sunucular[0]);

cout << "Sunucu Numaralari:\n";
listele(sunucular, boyut);
}

This example shows passing std::string as a parameter to the function.

MethodMemory ManagementSizeAdvantageDisadvantage
std::string dizi[]StaticFixedSimple and safeSize does not change
std::vector<string>Dynamic (STL)FlexibleAutogrowth, modernLight overhead
char dizi[][]Static (C-style)FixedCompatible with legacy systemsManual memory management

Frequently Asked Questions (FAQ)

  1. Why is it better to use std::vector for text string?

std::vector manages size dynamically. It prevents memory overflows and provides useful methods such as size().

  1. What is the disadvantage of the char[][] sequence?

Fixed length required for each text. Allocating too much space wastes memory, allocating too little causes truncation.

  1. Why might sizeof() give incorrect results in C++ text strings?

When the array is passed to the function, it becomes a pointer. sizeof in this case just returns the size of the pointer.

  1. What is the difference between std::vector<string> and std::array<string, N>?

std::vector is dynamically sized; std::array, on the other hand, is fixed size but shares the advantages of STL.


Result

There are multiple methods of creating text arrays in C++. Using std::vector<std::string> is the safest and most flexible approach in modern projects. std::string[] can be used in fixed size cases, and char[][] can be used for low level operations.

You can put these methods into practice by trying them on your own C++ example on the GenixNode platform.