0

How the return value becomes 0 here? And what does complier do when we reach at the end of a function? Anyone please!

https://code.sololearn.com/cG3d3WxPuWeY/?ref=app

11th Jan 2021, 1:31 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
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.
12th Jan 2021, 2:49 PM
Ipang
+ 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.
11th Jan 2021, 2:23 PM
Ipang
+ 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
11th Jan 2021, 1:50 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 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.
11th Jan 2021, 1:52 PM
Ipang
+ 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.
11th Jan 2021, 2:59 PM
Ipang
+ 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 👍
11th Jan 2021, 3:31 PM
Ipang
+ 2
Ipang Alphin K Sajan I found one more interesting thing today. I compiled this code on gcc. And it is working fine there.....!
12th Jan 2021, 1:32 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
+ 2
No problem bro 👌
12th Jan 2021, 2:58 PM
Ipang
+ 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?
11th Jan 2021, 1:58 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
+ 1
Alphin K Sajan Yes you're right,but Sorry, I didn't get a powerful answer.
11th Jan 2021, 2:00 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
+ 1
Okay. And could you pls tell me how the compiler works,at the end of a function.
11th Jan 2021, 3:23 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
+ 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
12th Jan 2021, 2:21 PM
Ipang
+ 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.
12th Jan 2021, 2:36 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
+ 1
Thanks a lot. And thanks for teaching me 1 more thing that I must 'Read warnings also.' :)
12th Jan 2021, 2:55 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
0
Then why if I return a variable from any function,and can access that successfully..(eg: return a)
11th Jan 2021, 2:54 PM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar