I'm new to C, just a question on returning a struct. I hear people saying that it is OK to return a struct. For example:
struct MyObj{
int x,y,z;
};
struct MyObj foo(){
struct MyObj foo_a;
foo_a.x = 10;
foo_a.y = 10;
foo_a.z = 10;
return foo_a;
}
int main () {
struct MyObj main_a = foo();
return 0;
}
My questions is:
foo_a is in foo's stack, so after the foo finish, the stack will be unwinded, which means foo_a doesn't actually exist in main function's stack, the pointer main_a that main holds is actually an illegal pointer, then how is it going to work?