Converting string array to const char*

I've seen and read plenty of examples on the web telling me how to convert from string to char, string to char array and so on, but nowhere have I seen anything about converting a string array to a const char*.
I've tried many methods to get this to work, and it's irritating as I'm only using this as validation to show that my previous code worked as expected (using the printf function).
Can anyone lend a hand?
In fact a string array can be considered as a 2-dimentional character array. So you can not convert a two-dimensional array to char *.
Last edited on
If you have a string array, you can turn each string into a const char* as you need them like this:

1
2
3
4
5
string arrayOfString[10]; // a string array
...
// populate those strings
...
const char* pointerToOneOfThem =  arrayOfString[1].c_str();
Or you can build a string from a string array by concatenating their contents and then apply member function c_str to the resulting string. For example

1
2
3
4
5
6
std::string a[] = { "This ", "is ", "a ", "test." };
std::string s;

for ( const auto &x : a ) { s += x; }

const char *ps = s.c_str();
Last edited on
for ( const auto &x : a ) { s += x; }
c++11 <3
Topic archived. No new replies allowed.