Returning const char*/ std::string.c_str()
Hi,
I have a function in a program that's something like the following. When I try to output what it returns, however, it doesn't print anything on the screen. The following isn't exactly what I'm using, but it gets the point across.
Code:
const char* getcolor(bool top)
{
stringstream color_convert;
string string1;
if (top == 1)
color_convert << 160;
else
color_convert << 200;
string1 = color_convert.str();
return string1.c_str();
}
Alternately, if I replace the contents of the function with:
Code:
const char* c_string1 = "160";
return c_string1;
then it will return something that I can print out. Does anyone know why this is? I'm guessing it's something to do with pointers- like in the first function, the pointer to the string is empty after it's returned or something? It's weird though because both functions use pointers to stuff. I don't see why one should be empty and the other not.
Thanks.