Attempting to target the more specific question asked in follow-up comments on several other answers:
if the address of the local variable after the scope gets deleted then how the value of that address preserved? in case of returning local variable value from the function? this is I can not understand
Returning a value makes a copy of that value, into a storage location provided by the caller. You presumably have something like
void call_func(void) {
int n = func();
printf("%d\n", n);
}
as the function that calls func
-- so, when func
returns the value of A
, that value gets copied into n
.
Returning a pointer to A
is actually exactly the same: a value is copied into a storage location provided by the caller. Only now the value that gets copied, is the address of the storage location formerly occupied by A.