Advertisement
Advertisement


std::string to char*


Question

I want to convert a std::string into a char* or char[] data type.

std::string str = "string";
char* chr = str;

Results in: “error: cannot convert ‘std::string’ to ‘char’ ...”.

What methods are there available to do this?

2011/09/08
1
349
9/8/2011 5:25:22 PM

Accepted Answer

It won't automatically convert (thank god). You'll have to use the method c_str() to get the C string version.

std::string str = "string";
const char *cstr = str.c_str();

Note that it returns a const char *; you aren't allowed to change the C-style string returned by c_str(). If you want to process it you'll have to copy it first:

std::string str = "string";
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
// do stuff
delete [] cstr;

Or in modern C++:

std::vector<char> cstr(str.c_str(), str.c_str() + str.size() + 1);
2016/09/20
692
9/20/2016 6:35:41 PM

More details here, and here but you can use

string str = "some string" ;
char *cstr = &str[0];
2019/07/31

If I'd need a mutable raw copy of a c++'s string contents, then I'd do this:

std::string str = "string";
char* chr = strdup(str.c_str());

and later:

free(chr); 

So why don't I fiddle with std::vector or new[] like anyone else? Because when I need a mutable C-style raw char* string, then because I want to call C code which changes the string and C code deallocates stuff with free() and allocates with malloc() (strdup uses malloc). So if I pass my raw string to some function X written in C it might have a constraint on it's argument that it has to allocated on the heap (for example if the function might want to call realloc on the parameter). But it is highly unlikely that it would expect an argument allocated with (some user-redefined) new[]!

2011/09/08

(This answer applies to C++98 only.)

Please, don't use a raw char*.

std::string str = "string";
std::vector<char> chars(str.c_str(), str.c_str() + str.size() + 1u);
// use &chars[0] as a char*
2017/01/20

  • If you just want a C-style string representing the same content:

    char const* ca = str.c_str();
    
  • If you want a C-style string with new contents, one way (given that you don't know the string size at compile-time) is dynamic allocation:

    char* ca = new char[str.size()+1];
    std::copy(str.begin(), str.end(), ca);
    ca[str.size()] = '\0';
    

    Don't forget to delete[] it later.

  • If you want a statically-allocated, limited-length array instead:

    size_t const MAX = 80; // maximum number of chars
    char ca[MAX] = {};
    std::copy(str.begin(), (str.size() >= MAX ? str.begin() + MAX : str.end()), ca);
    

std::string doesn't implicitly convert to these types for the simple reason that needing to do this is usually a design smell. Make sure that you really need it.

If you definitely need a char*, the best way is probably:

vector<char> v(str.begin(), str.end());
char* ca = &v[0]; // pointer to start of vector

This would be better as a comment on bobobobo's answer, but I don't have the rep for that. It accomplishes the same thing but with better practices.

Although the other answers are useful, if you ever need to convert std::string to char* explicitly without const, const_cast is your friend.

std::string str = "string";
char* chr = const_cast<char*>(str.c_str());

Note that this will not give you a copy of the data; it will give you a pointer to the string. Thus, if you modify an element of chr, you'll modify str.

2017/02/18

Source: https://stackoverflow.com/questions/7352099
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]