convert a string to a char array c++ code example
Example 1: string to char array c++
std::string myWord = "myWord";
char myArray[myWord.size()+1];//as 1 char space for null is also required
strcpy(myArray, myWord.c_str());
Example 2: COnvert string to char * C++
// CPP program to convert string
// to char array
#include <bits/stdc++.h>
using namespace std;
// driver code
int main()
{
// assigning value to string s
string s = "geeksforgeeks";
int n = s.length();
// declaring character array
char char_array[n + 1];
// copying the contents of the
// string to char array
strcpy(char_array, s.c_str());
for (int i = 0; i < n; i++)
cout << char_array[i];
return 0;
}
Example 3: c++ string to char array
const char *array = tmp.c_str(); //For const char array
char *array = &tmp[0]; // If you need to modify the array