Convert a std::string to char* in C++
This post will discuss how to convert a std::string to char* in C++. The returned array should contain the same sequence of characters as present in the string object, followed by a terminating null character (‘\0’) at the end.
1. Using const_cast Operator
We know that both string::c_str or string::data functions returns const char*. To get a non-const version, we can use the const_cast operator, which removes the const attribute from a class. This works in constant time as no copying is involved.
Please note that this approach will give us direct access to the underlying data structure (i.e., an array) behind std::string. That means any change to the char* will be reflected in the string object and vice versa.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string str = "std::string to char*"; char* c = const_cast<char*>(str.c_str()); std::cout << c; return 0; } |
Output:
std::string to char*
2. Using strcpy() function
Here, the idea is to pass the const char* returned by the string::c_str or string::data functions to the strcpy() function, which internally copies it into the specified character array and returns a pointer it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <string> #include <cstring> int main() { std::string str = "std::string to char*"; char* c = strcpy(new char[str.length() + 1], str.c_str()); std::cout << c; delete[] c; return 0; } |
Output:
std::string to char*
3. Using std::copy function
Using C‘s strcpy() function is not the C++-style. The recommended approach is to use the standard algorithm std::copy instead, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::string str = "std::string to char*"; int len = str.size(); char *c = new char[len + 1]; std::copy(str.begin(), str.end(), c); c[len] = '\0'; std::cout << c; delete[] c; return 0; } |
Output:
std::string to char*
4. Using std::vector function
We know that memory allocation of std::string is not guaranteed to be contiguous under the C++98/03 standard. The idea is to convert the string to a vector of chars whose memory allocation is contiguous. Then we can get a pointer to the underlying character array by invoking &str[0] or &*str.begin(). This approach is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <vector> int main() { std::string str = "std::string to char*"; std::vector<char> chars(str.begin(), str.end()); chars.push_back('\0'); char *c = &chars[0]; std::cout << c; return 0; } |
Output:
std::string to char*
5. C++11 – Contiguous storage of std::string function
Unlike C++98/03 standard, C++11 guarantees memory allocation of std::string to be contiguous. So, we can get a pointer to the underlying array behind std::string by invoking either the &str[0] or &*str.begin() function.
This works in constant time as no copying is involved. Please note that any change made to the char* will now be reflected in the string object and vice versa.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string str = "std::string to char*"; char* c = &*str.begin(); std::cout << c; return 0; } |
Output:
std::string to char*
That’s all about converting a std::string to char in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)