Is there any difference between the two functions below isn’t in both cases the address of local variable returned, but the using the f1() fn returns the correct value 4 but not f2(). Also in third case am I right about this that string constant are not stored in stack so when we return the pointer it will work fine. Also then where are string constant are stored?
int* f1()
{
int* a=(int*)4;
return a;
}
int* f2()
{
int a=4;
return &a;
}
char* f3()
{
char* p="abcd";
return p;
}
There are differences between all of them:
int *with the value 4, note that it doesn’t point to a variable with the value 4, but to the address 4.char *and notconst char *, one may try to modify the return buffer, which will, again result in undefined behavior.