Possible Duplicate:
string c_str() vs. data()
I use strncpy(dest, src_string, 32) to convert std::string to char[32] to make my C++ classes work with legacy C code. But does std::string’s c_str() method always return a null-terminated string?
Yes.
It’s specification is:
Note that the range specified for
iis closed, so thatsize()is a valid index, referring to the character past the end of the string.operator[]is specified thus:In the case of
std::string, which is an alias forstd::basic_string<char>so thatcharTischar, a value-constructedcharhas the value zero; therefore the character array pointed to by the result ofstd::string::c_str()is zero-terminated.