The C language allows the return value of a function to be a pointer (address), and we call such a function a pointer function. The following example defines a function Strlong (), which is used to return the longer one of the two strings:
#include <stdio.h>
#include <string.h>
Char *strlong (char *str1, char *str2) {
if (strlen str1 ) >= strlen (str2)) {return
str1;
} else{return
str2
}
}
int main () {
char str1[30], str2[30], *str;
Gets (STR1);
Gets (STR2);
str = Strlong (str1, str2);
printf ("Longer string:%s\n", str);
return 0;
}
Run Result:
C Language↙
C.biancheng.net↙
Longer string:c.biancheng.net
One thing to be aware of when using a pointer as a function return value is that when the function is run, it destroys all the local data defined within it, including local variables, local arrays, and formal arguments, and the pointers returned by the function should try not to point to the data, and the C language has no mechanism to guarantee that the data will always be valid. They may cause run-time errors during subsequent use. Take a look at the following example:
#include <stdio.h>
int *func () {
int n = m;
Return &n;
}
int main () {
int *p = func (), N;
n = *p;
printf ("value =%d\n", n);
return 0;
}
Run Result:
Value = 100
n is a local variable within the func (), Func () returns a pointer to n, according to the view above, N will be destroyed after the Func () run, and the value of n cannot be obtained using *P. But from the results of the operation, our reasoning seems to be wrong, func () after the end of the *p can still get the value of the local variable n, the above view is not inconsistent?
To further see the nature of the problem, you might want to modify the code slightly and add a function call between line 9th to 10th to see what happens:
#include <stdio.h>
int *func () {
int n = m;
Return &n;
}
int main () {
int *p = func (), N;
printf ("c.biancheng.net\n");
n = *p;
printf ("value =%d\n", n);
return 0;
}
Run Result:
C.biancheng.net
Value =-2
As you can see, p is now pointing to data that is not the value of the original n, it becomes a meaningless and even some weird value. Compared to the previous code, the code is just adding a function call before the *p, but the difference in the details results in a different operation, what is it?
We said before that the function will destroy all the local data, and this is true, and most of the C language textbooks also emphasize this point. However, the so-called destruction here is not to erase the memory of the local data, but the program to give up the use of its permissions, discard the Ignore, the following code can be free to use this block of memory. For the above two examples, the func () after the end of the operation of the N memory remains the same, the value is 100, if the use of timely and can get the correct data, if there are other functions are called will overwrite the memory, the data will be lost meaning.
With regard to the principle of function invocation and the more details of how functions occupy memory, we will delve into the topic of "C Language and Memory", and believe you will have an epiphany and unlock the mysteries of your heart.
The first example uses *p to get the value of N and save it before calling other functions, and the second example obviously fails to seize the opportunity to use *P to fetch the data after other functions have been invoked, which is late, and memory has been overwritten by later functions, And what kind of data is it that we can't infer (generally a value that is meaningless or even weird).
The above is the C language pointer as a function of the return value of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!