How is value getting retained even after function completing execution?
Undefined behavior (UB). It might appear to “work”, but that is not specified to be so by C.
fun(&ptr); runs fine, yet printf("%p",ptr); is UB as the value ptr is no longer valid. Many systems will tolerate this UB.
De-referencing ptr, with printf("%d\n",*ptr); is even worse UB. More likely to behave badly. Best not to attempt either of these.
solved Local variable values and Address getting retained even after function completes Execution [closed]