consider the following code:-
struct mystruct {
int data;
struct mystruct *next;
};
void myfunc ()
{
struct mystruct s1;
s1.data= 0;
s1.next = NULL;
myfunc2(&s1);
..
..
}
is it safe to pass the address of this local structure to other function.
Will this local structure be available for use outside the function or will it be already freed ?
It is safe to pass the address of a local variable to another function. The variable’s life time extends to the end of the block (function or compound statement) in which it is declared.
It is not safe to return the address of a local variable or save a pointer to it and use it after the declaring function has returned.