Function pointer, function pointer Definition

Source: Internet
Author: User

Function pointer, function pointer Definition
1. function pointer 1.1 function pointer:

Definition: pointer to a function. You can call the pointer to a function through the function pointer.

The pointer type must be the same as the data type to be pointed to. functions include: list of parameters of the return value function name.

Function pointer Declaration: Return Value Type (* function pointer name) (parameter list) = function name

The function name is the same as the array name.

// Example 1: int (* p) (int a, int B) = sumValue; // sumValue is a simple sum function of two numbers. // The variable name after * must be enclosed in parentheses printf ("% d \ n", p (5, 8 )); // result: 5 + 8 = 13 // Example 2. define a function to print "Hello" void (* p) () = printHello ;//The preceding figure is void.This is just a simple definition of p (); // function pointer call // example 3. define two functions, one for the least value, one for the sum, and the other for the maximum value of 3, 5 or char input [] = {0 }; scanf ("% s", input); int (* p) (int a, int B) = NULL; if (strcmp ("sumValue", input) = 0) {p = sumValue;} else if (strcmp ("maxValue", input) = 0) p = maxValue; printf ("% d", p (3, 5 )); // The result is correct. You can test it yourself.

 



1.2 callback function:

Callback Function Process: A bit similar to function nesting, but the callback function's parameter value is a function pointer. Through the function pointer, the corresponding function is called, and then the function pointer is returned.

For example, write a function to search for students with a score of 90 or higher and add "Gao fushuai" to the name using the callback function"

Student. h:

#import <Foundation/Foundation.h>typedef struct student{    char name[10];    float score;}Student;void search(Student *stu, void (*p) (Student *s));void addString(Student *s);

Student. m:
# Import "Student. h "void search (Student * stu, void (* p) (Student *) {if (stu-> score >=90) {p (stu );}} void addString (Student * s) {strcat (s-> name, "Gao fushuai ");}

 


Main. m:
int main(){    Student stu[3] = {{"zheng", 89}, {"chen", 91}, {"fang", 94}};    for (int i = 0; i < 3; i++) {        search(stu+ i, addString);        printf("%s   %.1f\n", stu[i].name, stu[i].score);    }    return 0;}

 


Result printing:
Zheng 89w.chen Gao fushuai 91.0fang Gao fushuai 94.0 Program ended with exit code: 0

 

1.3 dynamic sorting:
# Import <Foundation/Foundation. h> typedef struct student {char name [20]; int age; float score;} Student;# Pragma mark --------------- General Bubble Sorting START --------------------// Student name Ascending Order -------------------------------- void sortOfName (Student * stu, int count) {for (int I = 0; I <count-1; I ++) {for (int j = 0; j <count-1-I; j ++) {if (strcmp (stu [j]. name, stu [j + 1]. name)> 0) {Student temp = stu [j]; stu [j] = stu [j + 1]; stu [j + 1] = temp ;}}} for (int I = 0; I <count; I ++) {printf ("% s, % d, %. 1f \ n ", stu [I]. name, stu [I]. age, stu [I]. score) ;}}// age descending order ------------------------------ void sortOfAge (Student * stu, int count) {for (int I = 0; I <count-1; I ++) {for (int j = 0; j <count-1-I; j ++) {if (stu [j]. age <stu [j + 1]. age) {Student temp = stu [j]; stu [j] = stu [j + 1]; stu [j + 1] = temp ;}}} for (int I = 0; I <count; I ++) {printf ("% s, % d, %. 1f \ n ", stu [I]. name, stu [I]. age, stu [I]. score) ;}}// score in ascending order --------------------------------- void sortOfScore (Student * stu, int count) {for (int I = 0; I <count-1; I ++) {for (int j = 0; j <count-1-I; j ++) {if (stu [j]. score> stu [j + 1]. score) {Student temp = stu [j]; stu [j] = stu [j + 1]; stu [j + 1] = temp ;}}} for (int I = 0; I <count; I ++) {printf ("% s, % d, %. 1f \ n ", stu [I]. name, stu [I]. age, stu [I]. score );}}// ---------------------------------- END -------------------------------- # pragma mark --------------------- dynamic sorting START ---------------------------// Compare the size of the two age groups. The BOOL compareAge (Student * s1, Student * s2) {return s1-> age> s2-> age;} // compare the size of the two scores, in descending order, BOOL compareScore (Student * s1, Student * s2) {return s1-> score <s2-> score;} // sort BOOL compareName (Student * s1, student * s2) {return strcmp (s1-> name, s2-> name)> 0;} // compare two ages, descending order BOOL compareAge2 (Student * s1, student * s2) {return s1-> age <s2-> age;} typedef BOOL (* PFUN) (Student *, Student *); // PFUN = BOOL (*) (Student, Student)// Name the function pointer type PFUNVoid sortStudent (Student stus [], int count, PFUN pfun) {// This pfun can point to your own encapsulated sorting methods for (int I = 0; I <count-1; I ++) {for (int j = 0; j <count-1-I; j ++) {if (pfun (stus + j, stus + j + 1) = YES) {// call the sort method Student temp = stus [j]; stus [j] = stus [j + 1]; stus [j + 1] = temp ;}}for (int I = 0; I <count; I ++) {printf ("% s, % d, %. 1f \ n ", stus [I]. name, stus [I]. age, stus [I]. score) ;}} int main () {Student stu [5] = {"zhang", 23, 78}, {"zhao", 11, 89 }, {"bala", 22, 78.5 },{ "mouse", 7, 89 },{ "cat", 4,100 }}; // sortOfName (stu, 5 ); // sortOfAge (stu, 5); // sortOfScore (stu, 5); PFUN p = compareAge2; sortStudent (stu, 5, p ); // ------------------------------------- END -------------------------- return 0 ;}

 

1.4 function pointer:

Functions implemented by the Code: input any string of maxValue, minValue, addValue, and subtractValue to implement the functions of the corresponding code:

# Import <Foundation/Foundation. h> // four functions are used to obtain the maximum value, minimum value, sum, and int maxValue (int a, int B) {return a> B? A: B;} int minValue (int a, int B) {return a <B? A: B;} int addValue (int a, int B) {return a + B;} int subtractValue (int a, int B) {return a-B ;} typedef int (* PFUN) (int, int); // function pointer PFUNtypedef struct functionp {// struct, member: name: used to store the name of the corresponding function, func: store the address of the corresponding function char name [20]; PFUN func; // function pointer} Functionp; PFUN getFunctionByName (Functionp func [], int count, char funName []) {// string of the parameter struct array func [count], function string for (int I = 0; I <count; I ++) {if (strcmp (funName, func [I]. name) = 0) {// find the function pointer return func [I] in the corresponding struct. func ;}} return NULL;} int main () {Functionp f [4] ={{ "maxValue", maxValue}, // function string and function pointer {"minValue ", minValue },{ "addValue", addValue },{ "subtractValue", subtractValue }}; while (1) {char name [30] = {0 }; // The function string printf ("Input Function string: \ n"); scanf ("% s", name); getchar (); PFUN fun1 = NULL; // an empty function pointer fun1 = getFunctionByName (f, 4, name); if (fun1 = NULL) {printf ("please input again \ n ");} else printf ("% s = % d \ n", name, fun1 (3, 5);} return 0 ;}

 

Result:

Input Function string:
MaxValue
MaxValue = 5
Input Function string:
AddValue
AddValue = 8
Input Function string:
SubtractValue
SubtractValue =-2
Input Function string:

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.