In-depth understanding of pointer Functions
From http://blog.sina.com.cn/s/blog_5e8facd20100qn20.html
1. pointer Function Definition
As the name implies, a pointer function is a function that returns a pointer. The general definition form is as follows:
Type name*Function Name(Function parameter table column);
The suffix operator "()" indicates that this is a function. Its prefix operator asterisk "*" indicates that this function is a pointer function and its function value is a pointer, that is, the type of the value it brings back is a pointer. When this function is called, a "pointing to the return value is... Pointer (address),"Type name"Indicates the type of pointer to which the function returns ".
"(Function parameter table column) "Is the function call operator. In the call statement, even if the function does not contain parameters, a pair of brackets in the parameter table cannot be omitted. An example is as follows:
Int * pfun (int, int );
Because "*" has a lower priority than "()", pfun first combines with "()", which means that pfun is a function. That is:
Int * (pfun (int, int ));
Then, it is combined with the previous "*" to show that the return value of this function is a pointer. Since there is an int in front, that is, pfun is a function that returns an integer pointer.
Let's take a look at the differences between pointer functions and function pointers?
Int (* pfun) (int, int );
Using parentheses to forcibly combine pfun with "*" means that pfun is a pointer and then combined with "()", it indicates that the pointer points to a function, then it is combined with the previous int, that is, the return value of this function is int. It can be seen that pfun is a pointer to a function whose return value is int.
Although they have only one difference in parentheses, the meanings are completely different. The function pointer itself is a pointer that points to a function. The pointer function itself is a function, and its return value is a pointer.
2. Use the function pointer as the return value of the Function
In the pointer function mentioned above, there is such a type of function, they also return pointer data (address), but this pointer does not point to basic types such as int and char, but to the function. For beginners, not to mention writing such a function declaration, it is also confusing to see such a statement. For example, the following statement:
Int (* ff (int) (int *, int );
We use the method described above for analysis. ff is first combined with the following "()", that is:
Int (* (ff (int) (int *, int); // enclose ff (int) in parentheses.
This means that ff is a function.
Then combined with the previous "*", it means that the return value of the ff function is a pointer. Then it is combined with "()", that is, the pointer points to a function.
This writing style is really hard to understand, so that some beginners may misunderstand it and think that writing code that others cannot understand can show their high level. In fact, the opposite is true. Whether or not to write plain code is a standard for measuring whether programmers are excellent. Generally, using the typedef keyword makes the statement easier to understand. We have seen before:
Int (* PF) (int *, int );
That is to say, PF is a function pointer"Variable". When typedef is used for declaration, PF becomes a function pointer"Type", That is:
Typedef int (* PF) (int *, int );
In this way, the type of the return value is defined. Then, use PF as the return value to declare the function:
PF ff (int );
The following uses program listing 1 as an example to describe how to use a function pointer as the return value of a function. When the program receives user input, if the user inputs d, the maximum value of the array is obtained. If x is input, the minimum value of the array is obtained. If p is input, the average value of the array is obtained.
Example of how to calculate the maximum value and average value in program list 1
1 # include <stdio. h>
2 # include <assert. h>
3 double GetMin (double * dbData, int iSize) // calculates the minimum value.
4 {
5 double dbMin;
6 int I;
7
8 assert (iSize> 0 );
9 dbMin = dbData [0];
10 for (I = 1; I <iSize; I ++ ){
11 if (dbMin> dbData [I]) {
12 dbMin = dbData [I];
13}
14}
15 return dbMin;
16}
17
18 double GetMax (double * dbData, int iSize) // calculates the maximum value.
19 {
20 double dbMax;
21 int I;
22
23 assert (iSize> 0 );
24 dbMax = dbData [0];
25 for (I = 1; I <iSize; I ++ ){
26 if (dbMax <dbData [I]) {
27 dbMax = dbData [I];
28}
29}
30 return dbMax;
31}
32
33 double GetAverage (double * dbData, int iSize) // calculate the average value
34 {
35 double dbSum = 0;
36 int I;
37
38 assert (iSize> 0 );
39 for (I = 0; I <iSize; I ++)
40 {
41 dbSum + = dbData [I];
42}
43 return dbSum/iSize;
44}
45
46 double UnKnown (double * dbData, int iSize) // UnKnown Algorithm
47 {
48 return 0;
49}
50
51 typedef double (* PF) (double * dbData, int iSize); // define the function pointer type
52 PF GetOperation (char c) // obtain the operation type based on characters and return the function pointer
53 {
54 switch (c)
55 {
56 case 'D ':
57 return GetMax;
58 case 'X ':
59 return GetMin;
60 case 'p ':
61 return GetAverage;
62 default:
63 return UnKnown;
64}
65}
66
67 int main (void)
68 {
69 double dbData [] = {3.1415926, 1.4142,-0.5, 999,-313,365 };
70 int iSize = sizeof (dbData)/sizeof (dbData [0]);
71 char c;
72
73 printf ("Please input the Operation: \ n ");
74 c = getchar ();
75 printf ("result is % lf \ n", GetOperation (c) (dbData, iSize); // call a function through a function pointer
76}
In the above program, the first four functions are used to calculate the maximum, minimum, average, and unknown algorithms respectively, and then the GetOperation function is implemented. This function implements the above four functions based on the return values of the characters. It is returned in the form of a function pointer. It can be seen from the GetOperation (c) (dbData, iSize) of the main function, through which the function can be called.
From http://blog.sina.com.cn/s/blog_5e8facd20100qn20.html