I think that what is happening can be explained using the stack. When the function f is called, the stacks behave as follows. "ret" is the return address.
2| | 2| | 2| | 2| p | 1| | -> 1| | -> 1|ret| -> 1|ret| 0| | 0|100| 0|100| 0|100|
Now the stack pointer is 2, and this address (address p) is returned and assigned * x in main. When it returns from f, all stack values ββare unloaded, but in fact what happens is simply decreasing the value of the stack pointer. Therefore, the values ββthat were pressed still exist. The g function repeats, and the stack looks like this.
2| p | 2| p | 2| p | 2| y | 1|ret | -> 1|ret | -> 1|ret | -> 1|ret | 0|100 | 0|2500| 0|2500| 0|2500|
Same as function f, address p is returned and assigned * y in main. But note that this is also the same address as p, 2. Therefore, * x and * y are basically both pointing to the same place. The g function sets the values ββof this address to 2500, so both * x and * y print 2500.
Hope this helps. :)
source share