Possible Duplicate:
how to copy char * into a string and vice-versa
I have to pass a value into a method that required a char *
MyMethod(char * parameter)
{
// code
}
However, the parameter I need to feed it is currently a std::string. How do I convert the std::string to a char *?
std::string myStringParameter = //whatever
char * myCharParameter = //Convert here somehow
MyMethod(myCharParameter);
You are looking for the
c_str()method:You might need to
const_castthe return to get it into achar *instead of aconst char *. Refrain from modifying the string though; it is the internal buffer of thestd::stringobject. If you actually need to modify the string, you should usestrncpyto copy it to another buffer: