1. function as parameter plus * and no *
Example 1:
void print (int i) { printf ("Not parameter%d\n", i); } void Debugfun (void <span style= "color: #ff0000;" > (*funprint</span>) (int)) { funprint (2); Return
Main () { debugfun (print);}
Output:
Not parameter2
Example 2: (Remove the red part * number)
void print (int i) { printf ("Not parameter%d\n", i); } void Debugfun (void <span style= "color: #ff0000;" > (funprint</span>) (int)) { funprint (2); Return
Main () { debugfun (print);}
The output is the same as:
Not parameter2
Conclusion: The function pointer is the same as the formal parameter and does not add * meaning
The degree of rigor between 2.C and C + + for the type of function
C Example:
typedef struct REGISTER { int WR0; int wr1[4]; int wr2[4]; int wr3[4]; int WR4; int WR5; } Register;
void print (<span style= "color: #000000;" >register reg</span>) { printf ("Data entry is%d \ n", Reg.) WR0); }
void Listtraverse (void (*visit) (<span style= "color: #ff0000;" >Register</span>)) {
Reg. WR0 = 2; Visit (reg); }
Main () { listtraverse (print);}
Output:
Data item is 2
If you remove the red part Register, and the Red part visit (reg), change to visit (Reg. WR0), after, incredibly not error can also be correctly output;
Because the compiler automatically will be from Reg. WR0 the beginning of the content as the starting point of a register type (the length of sizeof (Register)) is converted to the register type as a function of the formal parameter
The following routines can prove what I said.
If you change into
void Listtraverse (void (*visit) ()) {
Reg. WR4 = ten; Visit (<span style= "color: #ff0000; Background-color: #f0f0f0 ">reg. wr4</span>); }
The output is:
Data item is 10
Obviously before the Reg. The content of the WR4 unit becomes the starting unit of the register type that is newly converted to
C + + Routines:
According to the code in the C routine compiler compiled with C + + compiler, if the parameters inside the pointer function does not add type identification, the program directly error
In summary, C and C + + are more stringent in the function pointers as function parameters to the formal parameter requirements of the function.
The difference between C and C + + function pointers as function parameters