Can I get a non-const C string back from a C++ string?

c, c++, string

Solution

I guess there is always `strcpy`.

Or use `char*` strings in the parts of your C++ code that must interface with the old stuff.

Or refactor the existing code to compile with the C++ compiler and then to use `std:string`.

Problem

Const-correctness in C++ is still giving me headaches. In working with some old C code, I find myself needing to assign turn a C++ string object into a C string and assign it to a variable. However, the variable is a `char *` and `c_str()` returns a `const char []`. Is there a good way to get around this without having to roll my own function to do it? edit: I am also trying to avoid calling new. I will gladly trade slightly more complicated code for less memory leaks.

Original source