Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare an array of strings in C++?

Tags:

c++

string

In C++ how can I declare an array of strings? I tried to declare it as an array of char but that was not correct.

like image 922
Thaier Alkhateeb Avatar asked Nov 28 '22 12:11

Thaier Alkhateeb


2 Answers

#include <string>

std::string my_strings[100];

That is C++, using the STL. In C, you would do it like this:

char * my_strings[100];

This reads as "my strings is an array of 100 pointer to char", and the latter is how strings are represented in C.

like image 83
unwind Avatar answered Dec 18 '22 08:12

unwind


I would rather recommend using a vector of strings in almost every case:

#include <string>
#include <vector>
std::vector<std::string> strings;
like image 43
soulmerge Avatar answered Dec 18 '22 06:12

soulmerge