1. The function pointer (important):
Definition: It is a pointer to the function; i.e. it is a form of the function pointer variable .
Format: Return Value Type (* variable name ) (Parameter Type 1, Type 2 ...... parameters);
example:
#include<stdio.h> // define the function function_1 void function_1 ( int , float ) { printf("%s\n", "function_1"); rreturn 0; } // define the function function_2 void function_2 ( int , float ) { printf("%s\n","function_2"); return 0; } // definition of function pointers pf int (* pf)(int ,float); // main function int main () { PF = function_1; // function pointer to function_1 PF ( 10 , 3.14 ); // call the function function_1 PF = function_2; // function pointer to a function_2 PF ( 2 , 2.12 ); // call the function function_2 return 0; }
Detailed: As described above, the function pointer is a special variable that points to a function. The general format of variable definitions: type variable name ; that is, for it is only normal before the variable name with a type on it.
But for the function pointer is on the complex, the need to add return type (* ) (Parameter Type 1, Type 2 parameters ......); can. I.e., return type (* ) (Parameter Type 1, Type 2 parameters ......); is a type of the function pointer variable.
2. pointer to the function:
Definition: It is a function of the type of the return value is a pointer.
Format: Return Value Type * function name (parameter type 1, type 2 parameter)
_type_ * function (int, int) with the normal function int function (int, int) Similarly, the data type is simply returned just not the same, _type_ * function (int, int) returns a pointer address, int function (int, int) It returns an int.
#include "stdio.h" #include "stdlib.h" int sum =0; int *getAdd(int a, int b) { sum = a+b; return ∑ } int getDiff(int a, int b) { return a>b?(a-b):(b-a); } int main () { int *pTemp, Temp; pTemp = getAdd(115,10); printf("ADD result:%d\n", *pTemp); Temp = getDiff(115,10); printf("DIFF result:%d\n", Temp); return 0; }