How to convert string to char massive

is any function to convert string to char*? not to const char*.
A C++ string is already an array of char. You can obtain a mutable pointer to it by taking the address of the first element:
1
2
std::string s = "abcdef";
char* p = &s[0];

There's also a member function data() that does the same
1
2
std::string s = "abcdef";
char* p = s.data();

(note, if your compiler/library is so old that it doesn't support C++11, this pointer cannot be passed into functions that expect C strings: that's why c_str() existed)
Last edited on
*Sigh* this is a duplicate of http://www.cplusplus.com/forum/general/236063/ . Please DON'T make multiple threads for the same question. You just waste people's time.
Topic archived. No new replies allowed.