Function returning a structure and function returning a string-- Different behaviour?
Hi,
I have heard that we cannot return a pointer to a local (automatic) array variable from a function since the automatic variable have limited scope and they die off once the called function returns.
Code:
char *s getName()
{
char name[]="Sumit"; // Automatic variable
retrun name; // No scope outside the function
}
But a doubt arises in my mind:
Code:
struct info getInfo(int a,int b)
{
struct info f1; // Automatic variable memory allocated for a structure
f1.a=a;
f1.b=b;
return f1;
}
here also we are returning a reference to a memory location which is allocated locally. Then how come here it works perfectly fine.
PLZ HELP
Waiting for reply