1. Function type
(1) A function in the C language has its own specific type, which is determined by the return value, the parameter type, and the number of parameters. such as int add (int i,int j) is of type int (int,int).
(2) Rename a function type in C language by typedef
typedef type name (parameter list);//as TypeDef int F (INT,INT);
2. Function pointers
(1) A function pointer is used to point to a function where the function name is the entry address of the executing function body.
(2) Two methods for defining function pointers
① is defined by the function type: functype* pointer;
② Direct definition: Type (*pointer) (parameter list);
Where type is the return value type, pointer is the function pointer variable name, parameter list is the parameter type
#include <stdio.h>typedefint(FUNC) (int);intTestinti) { returnIi;}voidf () {printf ("Call f () ... \ n");}intMain () {FUNC* pt = test;//legal, the function name is the entry address of the function body.//directly defines the function pointer, &f is the old-fashioned notation. The function name is just a symbol (not a variable),//As with the array name, no memory is allocated for it, so &f and F are equal in value. void(*PF) () = &f;//If you know the address of a function, this can be changed to a fixed address value, to achieve jump! printf ("PF =%p\n", PF); printf ("f =%p\n", F); printf ("&f =%p\n", &f);//The result should be: PF = = f = &f;pf ();//call with a function pointer (*PF) ();//old-fashioned notationprintf ("Function Pointer call:%d\n", PT (2)); return 0;}3. Callback function
(1) Callback function is a calling mechanism implemented by function pointers
(2) Principle of callback mechanism
① caller does not know the specific function to invoke when a specific event occurs
The ② function does not know when it is called, only the tasks that need to be done
③ when a specific event occurs, the caller invokes the specific function through the function pointer.
(3) The caller and callee in the callback mechanism are not dependent on each other.
The callback function uses the
#include <stdio.h>typedefint(*weapon) (int);//the function of manipulating a weapon//fight with a boss using some kind of weaponvoidFight (weapon WP,intArg//ARG is the parameter passed to the function pointer{ intresult =0; printf ("Fight boss!\n"); Result= WP (ARG);//call the callback function and pass in the parameter argprintf ("Boss loss:%d\n", result);//How much does boss bleed? }//using weapons--knivesintKnife (intN) { intRET =0; inti =0; for(i=0; i< N; i++) {printf ("Knife attack:%d\n",1); RET++; } printf ("\ n"); returnret; }//using weapons-swordsintSwordintN) { intRET =0; inti =0; for(i=0; i< N; i++) {printf ("Sword attack:%d\n",5); RET++; } printf ("\ n"); returnret; }//using weapons--gunsintGunintN) { intRET =0; inti =0; for(i=0; i< N; i++) {printf ("Gun attack:%d\n",Ten); RET++; } printf ("\ n"); returnret; }intMain () {Fight (knife,3);//cut it 3 times with a knife .Fight (Sword,4);//Stab 4 times with a sword .Fight (Gun,5);//shot 5 times . return 0;}
Function pointers are the key techniques for implementing callback mechanisms, and can be implemented in C programs with fixed address jumps
Resources:
Www.dt4sw.com
Http://www.cnblogs.com/5iedu/category/804081.html
C Language Learning notes--functions and pointers