Return pointer to temporary element C
Possible duplicate:
returns a pointer to a literal (or constant) character array (string)?
Is the code correct?
const char* state2Str(enum State state)
{
switch (state)
{
case stateStopped: return "START";
case stateRunning: return "RUNNING";
default: return "UNKNOWN";
}
}
printf("State is: %s\n", state2Str(stateRunning));
I'm worried that the function returns a pointer to a temporary object. What is the lifespan of such return values? Language - C89.
+3
source to share
2 answers