Possible Duplicate:
Can a local variable's memory be accessed outside its scope?
why does this function return garbage value
Why does this simple code return garbage?
char *output()
{
char o[2] = "A";
return o;
}
int main()
{
std::cout << output();
}
Because you return a pointer to invalid memory –
ois destroyed whenoutputreturns.You have several options:
malloc), copy"A"into this memory and return its addressreturn "A";P.S. Of course, you may use
std::stringand you will not have this issue. Or use in/out param, instead of return.