1. Simple function pointer Application
Form 1: return type (* function name) (number of rows table)
char (*pFun)(int); char glFun(int a){ return;} void main() { pFun = glFun; (*pFun)(2); }
The first row defines a pointer variable pfun. First, based on the aforementioned "Form 1", we realize that it is a pointer to a function. Such a function contains an int type and the return value is a char type. We cannot use this pointer in the first sentence, because we have not assigned a value to it.
The second row defines a function glfun (). This function is a function that returns char with int as the number of bytes. We need to understand the function at the pointer level-the function name is actually a pointer, and the function name points to the first address of the function code in the memory.
Then there is the main () function. You should have understood its first sentence-it assigns the address of the function glfun to the variable pfun. In the second sentence of the main () function, "* pfun" is obviously the content of the address pointed to by pfun. Of course, the content of the function glfun () is taken out, and the number of records is 2.
2. typedef is more intuitive and convenient.
Form 1: typedef return type (* New Type) (number of partitions table)
typedef char (*PTRFUN)(int); PTRFUN pFun; char glFun(int a){ return;} void main() { pFun = glFun; (*pFun)(2); }
The function of typedef is to define a new type. The first sentence defines a ptrfun type and defines such a type as a pointer to a function. Such a function takes an int as the number of bytes and returns the char type. Ptrfun can be used like Int or char.
The code in the second line defines the variable pfun using this new type, so that the variable can be used as in Form 1.
3. Example
# Include <stdio. h> # include <assert. h> typedef int (* fp_calc) (INT, INT); // defines a function pointer type int add (int A, int B) {return a + B ;} int sub (int A, int B) {return a-B;} int MUL (int A, int B) {return a * B;} int Div (int, int B) {return B? A/B:-1;} // defines a function. If the number of records is op, a pointer is returned, the pointer type is a function pointer that has two int values and the // return type is int. It returns the address of the corresponding function based on the operator fp_calc calc_func (char OP) {Switch (OP) {Case '+': Return add; Case '-': Return sub; case '*': Return Mul; Case '/': Return div; default: return NULL;} // s_calc_func is the function, and its number of records is op, // return value is a function pointer int (* s_calc_func (char OP) (INT, INT) {return calc_func (OP );} // Finally, the user calls the function directly. This function receives two int integers, // and an arithmetic operator, and returns the result int calc (int A, int B, char OP) {fp_calc fp = calc_func (OP); int (* s_fp) (INT, INT) = s_calc_func (OP ); // used for testing assert (FP = s_fp); // can assert that the two are equal if (FP) return FP (a, B); elsereturn-1 ;} void main () {int A = 100, B = 20; printf ("calc (% d, % d, % C) = % d \ n", A, B, '+', Calc (a, B, '+'); printf ("calc (% d, % d, % C) = % d \ n",, b, '-', Calc (a, B, '-'); printf ("calc (% d, % d, % C) = % d \ n ", a, B, '*', Calc (a, B, '*'); printf ("calc (% d, % d, % C) = % d \ n ", a, B, '/', Calc (a, B ,'/'));}
Exam:
Http://www.cnblogs.com/shenlian/archive/2011/05/21/2053149.html
Http://wenku.baidu.com/view/e9efb70879563c1ec5da7153.html
How to Use the typedef function pointer