Converting String to Cstring in C++

teamaster picture teamaster · Aug 6, 2012 · Viewed 113.2k times · Source

I have a string to convert, string = "apple" and want to put that into a C string of this style, char *c, that holds {a, p, p, l, e, '\0'}. Which predefined method should I be using?

Answer

Yann Ramin picture Yann Ramin · Aug 6, 2012

.c_str() returns a const char*. If you need a mutable version, you will need to produce a copy yourself.