As per the example that you have posted, this is what you are looking for:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string myArray[] = {"Apple", "Ball", "Cat"};
char test0[myArray[0].length()];
strcpy(test0, myArray[0].c_str());
char test1[myArray[1].length()];
strcpy(test1, myArray[1].c_str());
char test2[myArray[2].length()];
strcpy(test2, myArray[2].c_str());
int i=0;
for(i=0; i<(sizeof(test0)/sizeof(*test0)); i++)
cout<<test0[i]<<" ";
cout<<"\n";
for(i=0; i<(sizeof(test1)/sizeof(*test1)); i++)
cout<<test1[i]<<" ";
cout<<"\n";
for(i=0; i<(sizeof(test2)/sizeof(*test2)); i++)
cout<<test2[i]<<" ";
cout<<"\n";
return 0;
}
In the above code, I have created character arrays test0[]
, test1[]
and test2[]
of length equal to the corresponding string in myArray[]
. Then I used strcpy()
to copy the corresponding string from myArray[]
to the character array (test0[]
, etc). Finally, I just printed these new character arrays.
Working code here.
Note: I am assuming that you are using GCC, since it supports VLAs. If not, then you can use an array of particular length (or better yet, a vector
).
Hope this is helpful.