1. In C, a function is a function-to-pointer method, that is, a function is automatically converted to a pointer type.
Copy codeThe Code is as follows: # include <stdio. h>
Void fun ()
{
}
Int main (void)
{
Printf ("% p \ n", & fun, fun, * fun );
Return 0;
}
Bytes -------------------------------------------------------------------------------------------
The results of these three values are the same. in fact, for the last * fun, even if many * numbers are added, the results remain unchanged. That is, the results of ** fun and ** fun are the same.
This is because the function is a function-to-pointer method, which is automatically converted to the pointer type. & fun is the address of the function, which is the pointer type, fun is a function that converts to its pointer type. For * fun, since fun has become a pointer type and points to this function, * fun is a function that obtains this address, according to function-to-pointer, the function is also converted into a pointer, so the results of these three values are the same.
========================================================== ==============
2. How to call an address Function
If you know the address of a function, you can forcibly convert it into a function pointer of a certain type, and then call the function of this address based on this pointer. For example:
Copy codeThe Code is as follows: # include <stdio. h>
Void f (int I)
{
Printf ("I = % d \ n", I );
}
Int main (void)
{
Unsigned long add;
Add = (unsigned long) f;
(Void (*) (int) add) (10 );
(* (Void (*) (int) add) (20 );
Return 0;
}
Bytes ---------------------------------------------------------------------------------------
You can use (void (*) (int) to convert an address to a pointer type of a function with an int parameter and no return value, and then call it, because the function-to-pointer mentioned in, the effect of adding and not adding the * sign in the last two statements is the same. this method is often used in embedded systems.
========================================================== ==================
3. function pointer array usage.
Sometimes you need to define an array whose content is a series of function pointers and then call it, for example:Copy codeThe Code is as follows: # include <stdio. h>
Int max (int v1, int v2)
{
Return (v1> v2? V1: v2 );
}
Int min (int v1, int v2)
{
Return (v1 <v2? V1: v2 );
}
Int sum (int v1, int v2)
{
Return (v1 + v2 );
}
Copy codeThe Code is as follows: int main (void)
{
Int (* p [3]) (int, int );
P [0] = max;
P [1] = min;
P [2] = sum;
Printf ("p [0] = % d \ n", (p [0]) (3, 5 ));
Printf ("p [1] = % d \ n", (p [1]) (4, 6 ));
Printf ("p [2] = % d \ n", (p [2]) (1, 2 ));
Return 0;
}
Bytes -----------------------------------------------------------------------------------------
Although this method is a bit cumbersome, it is also a method of use, so let's introduce it.
========================================================== ====
4. Returns a pointer to an array.
The function can return a pointer to an array, for example:
Copy codeThe Code is as follows: # include <stdio. h>
# Include <stdlib. h>
Int (* p () [10]
{
Int (* m) [10];
Int I;
M = (int (*) [10]) malloc (10 * sizeof (int ));
If (m = NULL ){
Printf ("malloc error \ n ");
Exit (1 );
}
For (I = 0; I <10; I ++)
* (* M + I) = I + 1;
Return m;
}
Copy codeThe Code is as follows: int main (void)
{
Int (* a) [10];
Int I;
A = p ();
For (I = 0; I <10; I ++)
Printf ("% d", * (* a + I ));
Printf ("\ ndone \ n ");
Return 0;
}
-------------------------------------------------------------------
In this method, int (* a) [10] is a pointer to a one-dimensional array, and p () is also a pointer to a one-dimensional array.
========================================================== ==============
5. Returns a pointer to a function pointer.
/======================================================== =====/
/The function that returns the pointer is used in the Quick Sort example. So I found this article and think it is good .../
/======================================================== =====/
For this question, the signal () function is the best example.
Void (* signal (int signo, void (* func) (int );
Many of my friends didn't quite understand the definition of this function at the beginning. In fact, I can take a look at it step by step. I used to analyze it like this and hope it will be useful to everyone.
Int (* p )();
This is a function pointer. p points to a function that does not contain any parameters and returns an int value.
Int (* fun ())();
The difference between this formula and the above formula is that fun () is used to replace p, while fun () is a function. Therefore, after the function is executed, its return value is a function pointer. This function pointer (actually the above p) points to a function without any parameters and returns an int value.
Therefore, signal () can be regarded as the signal () function (it itself has two parameters, one is an integer, and the other is a function pointer), and the signal () the return value of a function is also a function pointer that points to a function with an integer parameter and the return value is void.
======================================
The signal function returns a pointer to the previous signal processing program. Therefore, an example is provided to illustrate how to return a pointer to the function.
Copy codeThe Code is as follows: # include <signal. h>
# Include <stdlib. h>
# Include <stdio. h>
Void sig_fun2 (int signo)
{
Printf ("in sig_fun2: % d \ n", signo );
}
Void sig_fun1 (int signo)
{
Printf ("in sig_fun1: % d \ n", signo );
}
Int main (void)
{
Unsigned long I;
If (signal (SIGUSR1, sig_fun1) = SIG_ERR ){
Printf ("signal fun1 error \ n ");
Exit (1 );
}
(Signal (SIGUSR1, sig_fun2) (30 );
Printf ("done \ n ");
Return 0;
}
========================================================== ================
6. Use function pointers as parameters (previously mentioned in the record .)
A function pointer may be included in function parameters, which appears in the signal () function.
In fact, in many sorting functions, this parameter is called using the function pointer. For example, Quicksort.
For example:
Copy codeThe Code is as follows: # include <stdio. h>
Int max (int v1, int v2)
{
Return (v1> v2? V1: v2 );
}
Int min (int v1, int v2)
{
Return (v1 <v2? V1: v2 );
}
Int sum (int v1, int v2)
{
Return (v1 + v2 );
}
Int fun (int a, int B, int (* call) (int, int ))
{
Return (call (a, B ));
}
Int main (void)
{
Printf ("max = % d \ n", fun (1, 2, max ));
Printf ("min = % d \ n", fun (3, 4, min ));
Printf ("sum = % d \ n", fun (5, 6, sum ));
Return 0;
}