IOS Study Notes-11th days in C Language

Source: Internet
Author: User

Function pointer

I. function pointer Definition

 

// Function declaration: declare what function I am. // calculate the sum of two numbers and // function type: int (int x, int y) // that is: I am a function that returns an integer and has two integer parameters. // The function name is sumint sum (int x, int y );

The function pointer defines p as a variable, and the others as a type (usually no form parameter a or B)

// Function pointer type int (*) (int x, int y) // Description: pointer type pointing to the two int parameters whose return values are int. // function pointer variable: p // Initial Value: sum printf ("% d", p (5, 8 ));

 

1 typedef int (* FP) (int x, int y); // at this moment, FP is int (*) (int x, int y)

Ii. Function callback

 

// Printf ("% d \ n", getValue (10, 8, sum); // printf ("% d \ n", getValue (10, 8, maxx); int sum (int x, int y) {return x + y;} // calculate the maximum value of two numbers int maxx (int x, int y) {int max = x> y? X: y; return max;} int getValue (int x, int y, int (* p) (int a, int B) {return p (x, y );}

Iii. Dynamic sorting

Variable sorting requirements

BOOL compareByAge (Student s1, Student s2); // compare the age of two students BOOL compareByScore (Student s1, Student s2 ); // compare the scores of two students: BOOL compareByAttendance (Student s1, Student s2); // compare the attendance of two students: BOOL compareByName (Student s1, Student s2 ); // compare the name sizes of two students: typedef BOOL (* CFP) (Student s1, Student s2); void sortArray (Student stus [], int count, cfp p ); // sort void printstudent (Student stus [], int count); // print the Student array // implement the function // compare the age of two students BOOL c OmpareByAge (Student s1, Student s2) {return (s1.age> s2.age )? YES: NO;} // compare the scores of two students. BOOL compareByScore (Student s1, Student s2) {return (s1.score> s2.score )? YES: NO;} // compare the attendance of two students. BOOL compareByAttendance (Student s1, Student s2) {return (s1.attendance> s2.attendance )? YES: NO;} // compare the names of two students. BOOL compareByName (Student s1, Student s2) {return strcmp (s1.name, s2.name)> 0? YES: NO;} // sort by age void sortArray (Student stus [], int count, cfp p) {for (int I = 0; I <count-1; I ++) {for (int j = 0; j <count-i-1; j ++) {if (P (stus [j], stus [j + 1]) {Student temp = stus [j]; stus [j] = stus [j + 1]; stus [j + 1] = temp ;}}}} // print the Student array void printstudent (Student stus [], int count) {for (int I = 0; I <count; I ++) {printf ("% s \ t %. 2f \ t % d \ t %. 2f \ n ", stus [I]. name, stus [I]. score, stus [I]. age, stus [I]. attendance) ;}}// main function Student stus [3] ={{ "lisi", 89.5, 0.5 },{ "zhangsan }, {"wangwu", 0.8, }}; printstudent (stus, 3); sortArray (stus, 3, compareByName); printstudent (stus, 3 );

4. the return value of a function is a function pointer.

 

// GetValue. code in h: typedef int (* PFUN) (int x, int y); // the old type of the new PFUN int (*) (int x, int y) // The ing table is a struct array. To create a ing table, we first create a struct. this struct contains a string and a function pointer struct NameFunctionPair {char name [30]; // string PFUN function; // function pointer}; typedef struct NameFunctionPair; // calculate the maximum value of 2 int maxValue (int x, int y); // calculate the minimum value of 2 int minValue (int x, int y); int sum (int x, int y); int minus (int x, int y); int multiple (int x, int y); int divide (int x, int y); int gcd (int x, int y); // maximum common divisor int gbs (int x, int y); // obtain the function name PFUN functionOfName (char * name) based on the string ); // The first two parameters are the numbers involved in the operation. The third parameter is used to query the ing table. // the return value is the result after the operation. The calculation depends on the third parameter. int getValue (int x, int y, char * name );
1 // GetValue. code 2 # import "GetValue. h "3 4 NameFunctionPair nfps [] = {5 {" max ", maxValue}, 6 {" min ", minValue}, 7 {" sum ", sum }, 8 {"minus", minus}, 9 {"mul", multiple}, 10 {"div", divide}, 11 {"gcd", gcd }, 12 {"gbs", gbs} 13}; 14 15 PFUN functionOfName (char * name) 16 {17 for (int I = 0; I <sizeof (nfps) /sizeof (nfps [0]); I ++) {18 if (strcmp (name, nfps [I]. name) = 0) {19 // If the ing table has a corresponding function, return this function20 Return nfps [I]. function; 21} 22} 23 // if not found, the maximum value is 24 return maxValue by default; 25} 26 int getValue (int x, int y, char * name) 27 {28 PFUN fun = functionOfName (name); // obtain the corresponding function 29 return fun (x, y) based on the name ); // use the selected Function Calculation Result 30} 31 32 int gbs (int x, int y) 33 {34 return x * y/gcd (x, y ); 35} 36 37 int gcd (int x, int y) 38 {39 while (x % y! = 0) {40 int temp = x % y; 41 x = y; 42 y = temp; 43} 44 return y; 45} 46 47 int divide (int x, int y) 48 {49 return x/y; 50} 51 52 int multiple (int x, int y) 53 {54 return x * y; 55} 56 57 int minus (int x, int y) 58 {59 return x-y; 60} 61 62 int sum (int x, int y) 63 {64 return x + y; 65} 66 67 int minValue (int x, int y) 68 {69 return x <y? X: y; 70} 71 72 int maxValue (int x, int y) 73 {74 return x> y? X: y; 75}
// Code # import <Foundation/Foundation. h> # import "GetValue. h "int main (int argc, const char * argv []) {/*** creates a ing table to store the string-function name pair when calling the function, check whether the given string is in the ing table. If yes, retrieve the corresponding function name and call the function to complete the result. */printf ("% d \ n", getValue (8, 12, "mul"); return 0 ;}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.