Just reading up on c_str() and notice that it returns a pointer to a c string.
So, for example, if I want to use this to obtain a string from a stringstream, would I use :
const char* p = ss.c_str(); or
string p = ss.c_Str(); ?
Both work, however because c_str() returns a pointer should I be using the former solution?
The two lines have different effects, so the right choice depends on what you want to do with the result.
If you need a C-style string (or a pointer to the first character of a C-style string) the first is the way to go. Note that p should not then be used in any way after any subsequent operation that changes ss.
If you need p to be a C++ string (std::string) copy of ss the second has that net effect (assuming the contents of ss are equivalent to a C-style string) but is more effectively done without a temporary C-style string as;
Right, thanks, I understand now.
Originally Posted by grumpy