pointer function int max (int a, int b);
* function pointer : pointer variable pointing to a function . '
* type of function : return value type + parameter
* function pointer variable type : Int (*) (int x, int y)
* The name of the pointer variable : max
* Initial value : NULL
* Max can point to a function that returns a value of type int and has a parameter of two int .
application: student structure by name, age, score ascending order
1. Declaration of functions
typedef struct student{ char name[20]; int age; float score;} Student;typedef BOOL (*all) (Student, Student); typedef struct namefunctionpair{ char name[10];//name all p;// The corresponding function address}namefunctionpair;//age bool Comparebyage (Student stu1, Student stu2);//score bool Comparebyscore (Student STU1, Student STU2);//name bool Comparebyname (Student stu1, Student stu2);//Sort void sortstudent (Student *p, int cont, all a);//Output There is information about the student void Outputstudent (Student *p, int count);//matches the corresponding function address from the matching table. All Matchfunctionbyname (Namefunctionpair *q, int count, char *name),//*p receives a matching table array. Count. Accepts the number of elements of the matching table array. name. Accept user input. <span style= "color: #ff0000;" ></span>
2. Implementation of functions
Output information for all students void Outputstudent (Student *p, int count) {for (int i = 0; i < count; i++) {printf ("%s,%d,%.1f\n ", P[i].name,p[i].age,p[i].score); }}//age bool Comparebyage (Student stu1, Student stu2) {return stu1.age > stu2.age;} score bool Comparebyscore (Student stu1, Student stu2) {return stu1.score > stu2.score;} name BOOL Comparebyname (Student stu1, Student stu2) {return strcmp (Stu1.name, stu2.name) > 0;} sort void Sortstudent (Student *p, int count, all a) {for (int i = 0; i < count-1; i++) {for (int j = 0 ; J < count-1-I; J + +) {if (A (p[j], p[j + 1])) {Student temp = p[j]; P[J] = p[j + 1]; P[j + 1] = temp; }}}}all Matchfunctionbyname (Namefunctionpair *q, int count, char *name) {for (int i = 0; i < CO UNT; i++) {if (strcmp (q[i].name, name) = = 0) {return Q[I].P; }} return null;//if no match succeeded in returning NULL.}
3. Function implementation
Student Stu[5] = { {"zhangs", +, 78.9}, {"Xiaom", 45.8}, {"Xiaozhen ", 19,56.0}, {"Heh", 20, 64.8},< c7/>{"Frak", 79.0} }; Namefunctionpair Name[3] = {{"Age ", comparebyage}, {"Score", Comparebyscore}, {"Name", Comparebyname } }; while (YES) { char temp[10] = {0}; printf ("name-name, age-age, socre-score: \ n"); scanf ("%s", temp); Match the user input to the corresponding function all al = Matchfunctionbyname (name, 3, temp); if (Al! = null) {//while (P = = null) //Sort According to what is matched to. Sortstudent (Stu, 5, AL); Outputstudent (Stu, 5); } else if (Al = = NULL) { printf ("The string you entered is not correct!), please re-enter \ n");}
Two. callback function application.
1. Randomly generate an array of ten elements , find a multiple of 3 , and modify its value to 0. ( Note : modifying values using the rollback function processing )
1. Function declaration
typedef int (*zero) (int,int);
A random array of 10 elements void Gettennumber (int *p, const int count);//traverse remove element void Outputnumber (int *p, const int count);//Find multiples of 3, and is assigned a value of 0void findnumbertozero (int *p, const int count, ZERO z, int n);//Find multiples of n, int copyzero (int number,int n);
2. Method implementation
Array of random 10 elements void Gettennumber (int *p, const int count) {for (int i = 0; i < count; i++) { P[i] = Arc4random () % (40-10 + 1) + Ten; }} Traversal of the number of elements to be removed void outputnumber (int *p, const int count) {for (int i = 0; i < count; i++) { printf ("%d", P[i]);
}}//found a multiple of 3 and was assigned a value of 0void findnumbertozero (int *p, const int count, ZERO z,int N) {for (int i = 0; i < count; i++) { if (z (p[i],n) = = 0) { P[i] = 0 ; }}} int Copyzero (int number, int n) { if (n = = 0) { return NO; } return number% n;}
3. Function calls
int arr[10] = {0}; Gettennumber (arr, ten); Outputnumber (arr, ten); 4 corresponds to find a multiple of 3, Findnumbertozero (arr, Copyzero, 4); printf ("\ n"); Outputnumber (arr, 10);
"Learning the path of iOS: C language" pointer function