|
A pointer to a function:
#include "StdAfx.h"
?
#include <stdio.h>
#include "T.h"
?
void Function (int i)
{
???? printf ("Call Function:%d\n", i);
}
?
void F1 ()
{
???? typedef void (*pfunc) (int);
???? PFUNC PFUNC;
???? Pfunc = Function;
???? Pfunc (1);????
????
???? Pfunc = &Function;
???? Pfunc (2);
}
?
?
void F2 ()
{
???? typedef void (FUNC) (int);
????
???? func* Pfunc;
???? Pfunc = Function;
???? Pfunc (3);
?
???? Pfunc = &Function;
???? Pfunc (4);????
?
}
?
void T2 ()
{
???? F1 ();
???? F2 ();
}
/*
Call Function:1
Call Function:2
Call Function:3
Call Function:4
Please press any key to continue . . .
*/
Second, function pointer disassembly:
#include <signal.h>
void (*signal (int signo,Void (*func) (int)))) (int);
?
1 , using typedef Disassembly
typedef void (*sighandler_t) (int);
sighandler_t signal (int signum,sighandler_t handler);
?
2 , using function pointers to understand
Int (*p) ();
P point to a parameter without any parameters, and the return value is int the function;
Int (*fun ()) ();
the difference between this formula and the above is Fun () Replace P , i.e. Fun () The return value is a function pointer, the function pointer ( can be seen as p) , point to one without any parameters, and return a value of int function;
void (*signal (int signo,void (*FUNC) (int)))) (int);
can be seen as signal () function, this function takes two parameters: one is an integer and one is a function pointer. The return value of this signal () function is also a function pointer, which points to a function with an integer parameter and a return value of void .
?
This function is too complex to have any meaning in itself and is usually used typedef after the simple signal processing function.
?
3 , Case analysis
1 , instance source code
?
2 , execution procedures
?
3 , view process number
?
4 , send a signal to the process
?
5 , the process receives the signal ?
??
Three,(* (void (*) ()) 0) ()------ What is this?
Do you feel the above example is too simple, not exciting? OK, let's have some excitement, look at the following example:
/colgroup>
|
1 |
(* (void (*) ()) 0) (); |
This is the C Traps and pitfalls An example of this classic book. Not crazy, huh? Let's analyze and analyze:
|
1
2
3
4 |
The first step: void (*) (), you can see that this is a function pointer type. This function has no parameters and no return value.
The second step: (Void (*) ()) 0, which is to cast 0 to the function pointer type, 0 is an address, that is, a function exists within a section of the first address of 0.
Step Three: (* (Void (*) ()) 0), which is the contents of a section of memory starting at 0 address, which is the function stored in a section of the first address of 0.
Fourth Step: (* (Void (*) ()) 0) (), this is a function call. |
Seems to be very simple, right, the above example rewrite rewrite:
/colgroup>
|
1 |
(* (char** (*) (char?**,char?**)) 0) (? char?**,char?**); |
If there is no analysis above, Ken is afraid of not easy to see this expression to understand it. But it should be a very simple thing now. What does the reader think?
?
?
Reference:
1. Anatomy of "void (*signal (int) signo,void ( int))) (int)" http:// www.51hei.com/bbs/dpj-29464-1.html ;
2. the understanding and use of C-language function pointers;
|