char* f()
{
char s[100];
//....function body code
return s;
}
Why should it not be written like this?
char* f()
{
char s[100];
//....function body code
return s;
}
Why should it not be written like this?
s
is a local variable that only exists within the function.
Once the function exits, s
no longer exists, and its memory will be re-allocated to other parts of your program.
Therefore, your function is returning a pointer to a random meaningless block of memory.