A function to implement a specific function of the code
1) function name is the same as the array name is the address
2) pointers to functions can be called by function pointers to functions that are pointed to by function pointers
3) return value type (* function pointer name) (parameter type) = function name
int maxValue (int A,int b) { return a > B a:b;} int (*p) (int,int) = maxvalwe; printf ("%d\n", p (3,4)); // to invoke a function with a pointer
4) Sample Code
intMaxValue (intAintb) { returna > B?a:b;}intSumvalue (intAintb) { intsum = a +b; returnsum; }intMainintargcConst Char*argv[]) { int(*p) (int,int) =NULL; intn =0; scanf ("%d",&N); Switch(n) { Case 1: P=MaxValue; Break; Case 2: P=Sumvalue; Break; default: printf ("Not in service"); Break; } if(p!=NULL) {printf ("%d", P (3,5)); } return 0; }
5) According to the grade of students in ascending order of 90 above the name of the height of the rich handsome
typedefstructstudent{Charname[ -]; Chargender; intAge ; floatscore;} Student;voidPrintChar*name) {strcat (name,"Gaofu");}voidFindvalue (Student *s,intCountvoid(*p) (Char*)){ for(inti =0; i<count; i++) { if(S[i].score > -) {p (s[i].name); } printf ("%s%c%d%f\n", S[i].name,s[i].gender,s[i].age,s[i].score); }}intMainintargcConst Char*argv[]) {Student stu[3] = {{"Xiao",'m', at, -},{"Ming",'F', -, the},{"Zhang",'m', -, the}}; Student*s =Stu; Findvalue (s),3, print); return 0; }
6) Realization of dynamic arrangement of functions
typedefstructstudent{Charname[ -]; Chargender; intAge ; floatscore;} Student;typedef BOOL (*sortall) (Student stu1,student stu2);//Defining a function pointer typevoidSortstudent (Student *s,intCount,sortall al)//function pointer pointing to function (Sortage){ for(inti =0; I < count-1; i++) { for(intj =0; J < count-1-I.; J + +) { //function Callback if(Al (s[j],s[j+1]) {Student temp=S[j]; S[J]= s[j+1]; S[j+1] =temp; } /*through the function pointer al pointer function (sortage) * So s[j],s[j+1] These two parameters also point to the function (sortage) of the shape parametric stu1,stu2 if (Al (S[j]), s[j+1]) is equivalent to if (S[j].age > S[j+1].age)*/ } } for(inti =0; i<count; i++) {printf ("%s%c%d%.2f\n", S[i].name,s[i].gender,s[i].age,s[i].score); }}bool sortage (Student stu1,student stu2) {returnStu1.age >stu2.age;}intMainintargcConst Char*argv[]) {Student stu[3] = {{"Xiao",'m', at, -},{"Ming",'F', -, the},{"Zhang",'m', -, the}}; Student*s =Stu; Sortstudent (s),3, Sortage); return 0; }
C Language Basics _ function pointers