C language in the function of the return value of the problem, in the function of the local variables are mainly on the stack open, out of the function variable is recycled, for the function return is worth the problem, give the following a few more specific examples to illustrate:
function return value is a local variable defined in the function
This type of return value can be used in the main function, because it is worthwhile to return a local variable, it is worth a copy, and in the main function we need only this value, so it is possible, for example
int fun (char *arr)
{
int num = 0;
while (*arr! = ' \\0 ')
{
num = num * + *arr-' 0 ';
arr++;
}
return num;
printf ("%d", num);
}
int main ()
{
int tem = 0;
Char *arr = "12345";
tem = Fun (arr);
printf ("%d", tem);
System ("pause");
return 0;
}
2. The function returns the pointer variable defined in the function
Char *fun ()
{
Char *arr = "1234";
return arr;
}
int main ()
{
Char *tem = fun ();
printf ("%s", tem);
System ("pause");
return 0;
}
This is also true in the process of operation.
3. Function cannot return the address of a local variable
int *fun ()
{
int a = 10;
Return &a;
}
int main ()
{
int *tem = Fun ();
printf ("%d", *tem);
System ("pause");
return 0;
}
4. Function also cannot return the first address of an array
int *fun ()
{
int arr[] = {1, 2, 3, 4};
return arr;
}
int main ()
{
int *tem = Fun ();
System ("pause");
return 0;
}
This article is from the "local and static Variables" blog, reproduced please contact the author!
The problem of function return value in C language