main
, (ii) by calling an auxiliary function, (iii) by assigning the address of the A from (i), and by using malloc
in an auxialiary function. The syntax for access to the member elements is different in the case of a pointer to the struct variable and a struct variable itself.
As you can see from the output, all four approaches give valid A's but the last one has a quite different address. It is on the "heap" rather than on the "stack."
One thing I don't understand is whether examples 2 and 4 are dangerous. Under what circumstances do objects that we obtain from a function go away when those functions return? If you know, I'd be grateful for your insight. [UPDATE: I'm pretty sure example 2 is not something you should do, because after the function returns that memory will be reallocated. I guess we got away with it here because no further allocations were made.]
Output:
Code listing: