How to return a local char array from a function which has the return type as const char*?
Note: I want a way without using std::string
const char * getXXX()
{
char buf[32];
sprintf(buf,"XXX%d",i); // some manipulation
return buf; // How to return???
}
std::string? Using the standard library will make your life as a C++ programmer so much easier!std::string- then you can simply return it by value and don't have to worry about freeing allocated memory.std::ostringstream? Using it you can use all the normal C++ stream output formatting functionality, and still get astd::stringas output. No fixed-size buffers, no special case handling for strings, type safety, automatic appending to the string. All of those things you can't get by using old C-stylesprintfand family.