I have read a few posts about function return values. I think it is necessary to learn more about some underlying programming languages.
I have summarized some of the following. It must be incomplete. I hope you can correct O (∩ _ ∩) O ~
When we use a called function, return is divided into two situations: one is the return value and the other is the pointer (or reference)
When we return a value, the code is compiled successfully, and the output result is correct. The following code passes the test.
#include <stdio.h>int sum(int a,int b){ int s; s=a+b; return s;}int main(void){ int a,b; scanf("%d%d",&a,&b); printf("%d\n",sum(a,b)); return 0;}
Resolution: when the main function (that is, the main function) calls the sum function, the partial variable s of the sum function is the sum of the two real parameters, which is stored in the stack, at this time, a stack operation will be performed.
When the function call ends, that is, return, the local variable is destroyed as the function ends, but s is destroyed, and the value is returned to the main function through a copy, that is, an out-of-stack operation is performed.
In fact, the essence is to return a copy of the value, that is, the sum of two numbers. This is a real number.
So the running result is OK.
However, if we want to return a pointer, the problem may occur.
For example, the following code
#include <stdio.h>char*GetMemory(void){ char p[]="hello world"; return p;}int main(void){ char *str=NULL; str=GetMemory(); printf(str); return 0;}
Analysis: I saw a very vivid explanation in other places, that is, when you travel, you think that the food in a local hotel is very distinctive, so I noted down its address room number (equivalent to a pointer). When you go back, that is, the function call is complete, it is no problem to return this pointer, after you go back, you can still check the house number of this address. However, when you go back to the last tour, the hotel may no longer exist and may become a barber shop or something.
Therefore, when the function call ends, the content pointed to by the pointer is stored in the stack and will be destroyed along with the function, but the pointer still exists, but the content pointed to by the pointer is not clear at this time, therefore, the result will be wrong (it is the same as passing back the above value, passing a copy of the pointer back to the main function, but the pointer is pointing to the local variable of the called function, this local variable has been destroyed along with the called function, so the pointer pointing is unknown, at least unknown, so it is prone to errors)
However, we can avoid the error returned by the pointer through other methods.
For example, to extend the life cycle of a pointer, set it as a static variable to extend its life cycle to the entire file.
Or set it as a String constant so that it can be stored in the static zone.
The implementation code is as follows:
#include <stdio.h>char *GetMemory(void){static char p[]="hello world";return p;}int main(void){printf("%s\n",GetMemory());return 0;}
#include <stdio.h>char *GetMemory(void){ char *p="hello world";return p;}int main(void){printf("%s\n",GetMemory());return 0;}
You are also welcome to criticize and correct the shortcomings, so that you can learn and make progress together.