"how to convert string to char array in c++?" Code Answer


simplest way i can think of doing it is:

string temp = "cat";
char tab2[1024];
strcpy(tab2, temp.c_str());

for safety, you might prefer:

string temp = "cat";
char tab2[1024];
strncpy(tab2, temp.c_str(), sizeof(tab2));
tab2[sizeof(tab2) - 1] = 0;

or could be in this fashion:

string temp = "cat";
char * tab2 = new char [temp.length()+1];
strcpy (tab2, temp.c_str());
By Vladislav Zorov on September 19 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.