0
How the return value becomes 0 here? And what does complier do when we reach at the end of a function? Anyone please!
15 Answers
+ 2
Yes, you are receiving a value back from function fun(), but if the compiler was configured to give warnings about possible problem (like in SoloLearn Code Playground), you will also receive warnings.
I see 2 warnings for the code as I ran it:
1. Warning for returning address of local variable.
2. Warning for inappropriate use of format specifier %u for a pointer (use %p for pointers).
Using %p format to print the pointer you may see (nil) as output which tells you that the pointer is not good to be dereferenced.
+ 4
As I understand it, resources used, including memory for local variables in functions are destroyed before control is returned to the caller (by issuing return statement).
Although I don't know for sure what goes under the hood, I think a zero (NULL) is returned because the address &a is no longer valid.
Try printf("%p", p); to see if the returned pointer was good for dereferencing.
+ 2
<Saurabh Singh Rawat/> Since the compiler starts execution from main() and the return type is specified as int ,So returning an integer is must and by convention we return 0 indicating the program is successful . Kindly use the Search bar ...
https://www.sololearn.com/discuss/58866/?ref=app
https://www.sololearn.com/discuss/315777/?ref=app
https://www.sololearn.com/discuss/97686/?ref=app
https://www.sololearn.com/discuss/53497/?ref=app
https://www.sololearn.com/discuss/108138/?ref=app
https://www.sololearn.com/discuss/838600/?ref=app
+ 2
You are returning address of local variable <a> which is only recognized in `fun` function. As function `fun` ends and returns control to `main`, variable <a> is destroyed.
+ 2
You can return a value of a local variable (the value is copied), but not a pointer to its address. This is what I understood this far.
+ 2
Sorry, I think such details are far beyond my knowledge. All I can say is that all local resources used are destroyed before return statement.
Maybe you can try to find the source code of the compiler to get better understanding 👍
+ 2
Ipang
Alphin K Sajan
I found one more interesting thing today. I compiled this code on gcc. And it is working fine there.....!
+ 2
No problem bro 👌
+ 1
Ipang
You're right: as function ends 'a' is destroyed. And that's why I can't access value '10' from main ().
But before fun () gets destroyed I also return '&a' so why it go vein too?
+ 1
Alphin K Sajan
Yes you're right,but
Sorry, I didn't get a powerful answer.
+ 1
Okay.
And could you pls tell me how the compiler works,at the end of a function.
+ 1
Saurabh,
That is because variable <a> is now set as static. static variables are stored in a different place in memory than that of local variables, so it doesn't get destroyed like local variables.
For more about static modifier in C language 👇
https://www.c-programming-simple-steps.com/static-keyword-in-c.html
+ 1
Opss I am really sorry, I made some changes, now pls check it is as it was! And still I am getting address from function in gcc.
+ 1
Thanks a lot.
And thanks for teaching me 1 more thing that I must 'Read warnings also.'
:)
0
Then why if I return a variable from any function,and can access that successfully..(eg: return a)