• 函数指针和回调函数


    1函数指针
    声明一个max函数
    int max(int a,int b);
    函数的定义
    int (*p)(int ,int );//函数指针的定义 构成:返回值类型 函数指针的名字 (参数的类型)
    2回调函数
    int getValue(int a,int b,int (*p)(int , int));//回调函数声明(函数参数里面有一个函数指针)
    int getValue(int a,int b,int (*p)(int ,int )){//回调函数的定义 使用函数指针做参数 你可以不管函数的具体代码 使用指针直接调用即可
    return p(a,b);}//如果说p指向的是一个核心的一个代码函数 如果这么使用 回调函数可以起到保护代码的作用 类似于OC中封装的特性
    3动态排序
    //练习 使用函数指针 给学生数组s1:"aaa",17,80 s2:"bbb",18,70 s3:"ccc",22,60 s4:"ddd",15,90 分别将其年龄,成绩,姓名排序,输出
    typedef struct student{
        char name[20];
        int age;
        float score;
    }student;
    BOOL cmpByAge(student *s1,student *s2);//函数的声明
    BOOL cmpByAge(student *s1,student *s2){//函数的定义
        return s1->age > s2->age;
    }
    BOOL cmpByScore(student *s1,student *s2);
    BOOL cmpByScore(student *s1,student *s2){
        return s1->score > s2->score;
    }
    BOOL cmpByName(student *s1,student *s2);
    BOOL cmpByName(student *s1,student *s2){
        return strcmp(s1->name, s2->name) > 0;
    }
    void sortArr(student stuArr[],int n , BOOL (*p)(student *,student *));
    void sortArr(student stuArr[],int n , BOOL (*p)(student *,student *)){
        for (int i = 0; i < n - 1; i++) {
            for (int j= 0; j < n - 1 - i; j++) {
                if (p(&stuArr[j],&stuArr[j+1])) {//利用函数指针进行比较
                    student temp = stuArr[j];
                    stuArr[j] = stuArr[j+1];
                    stuArr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            printf("%s %d %.2f ",stuArr[i].name,stuArr[i].age,stuArr[i].score);
        }
    }
    int main(int argc, const char * argv[]) {
        student stuArr[4] = {{"aaa",17,80},{"bbb",18,70},{"ccc",22,60},{"ddd",15,90}};                
        BOOL (*p1)(student *,student *) = NULL;
        p1 = cmpByAge;
        BOOL (*p2)(student *,student *) = NULL;
        p2 = cmpByScore;
        BOOL (*p3)(student *,student *) = NULL;
        p3 = cmpByName;
        sortArr(stuArr, 4, p1);
        printf(".......... ");
        sortArr(stuArr, 4, p2);
        printf(".......... ");
        sortArr(stuArr, 4, p3);
        printf(".......... ");
    4函数返回值是函数指针
    //typedef 给函数指针作为返回值的函数起新名字的两种形式
    //1
    //typedef int (*p1) (int ,int );
    //p1 func1(char name[20]);
    //2
    //typedef int p2(int ,int );
    //p2 *func2(char name[20]);
    //Block
    1
    //float (*p1)(float , float);
    //double (*p2)(double , double);
    ////void (*p3)(stu *,stu *);
    //int max(int a,int b){
    //    return a > b ? a : b;
    //}
    //int sum(int a,int b);
    //int sum(int a,int b){
    //    return a+b;
    //}
    //float Sum(float a,float b);
    //double Swap(double a, double b);
    ////void name(stu *s1,stu *s2);
    //void (*p4)();//1111111
    //void printHello();
    //void printHello(){
    //    printf("hello");
    //}
    //void printHi(){
    //    printf("hi");
    //}
    //int (*p)(int , int );
    //int max(int a,int b);
    //int max(int a,int b){
    //    return a > b ? a : b;
    //}
    //int sum(int a,int b);
    //int sum(int a,int b){
    //    return a + b;
    //}
    2
    //typedef struct student{
    //    char name[20];
    //    int score;
    //}student;
    //void addName(student *s);//void addName(student *s,int count);
    //void addName(student *s){
    //    char y[20] = "抢头条王";
    //    strcat(s->name, y);
    //}
    //void AddName(student *s);
    //void AddName(student *s){
    //    char x[20] = "高富帅";
    //    strcat((*s).name, x);
    //}
    //void findStu(student *arr,int count ,void (*ps)());
    //void findStu(student *arr,int count ,void (*ps)()){
    //    for (int i = 0; i < count; i++) {
    //        if (arr[i].score >= 90) {
    //            ps = AddName;
    //            ps(arr[i].name);
    //        }
    //        else if (arr[i].score <= 60){
    //            ps = addName;
    //            ps(arr[i].name);
    //        }
    //    }
    //    for (int i = 0; i < count; i++) {
    //        printf("%s %d ",arr[i].name,arr[i].score);
    //    }
    //}
    //void (*p)() = NULL;
    typedef struct student{
        char name[20];
        int score;
    }student;
    void addName(student *s);
    void addName(student *s){
        strcat(s->name, "高富帅");
    }
    void findStu(student stuArr[],int n,void (*p)(student *));
    void findStu(student stuArr[],int n,void (*p)(student *)){
        for (int i = 0; i  < n; i++) {
            if (stuArr[i].score >= 90) {
                p(&stuArr[i]);
            }
        }
    }
    int main(int argc, const char * argv[]) {
        student stuArr[4] = {{"周杰伦",98},{"庾澄庆",80},{"那英",70},{"汪峰",30}};
        void (*p)(student *) = NULL;
        p = addName;
        findStu(stuArr, 4, p);
        for (int i = 0; i  < 4; i++) {
            printf("%s %d ",stuArr[i].name,stuArr[i].score);
        }
       // student arr[4] = {{"周杰伦",98},{"庾澄庆",70},{"那英",80},{"汪峰",30}};
    //    student *parr = arr;
    // //   p = addName;
    //    findStu(parr, 4, p);
    //    for (int i = 0; i < 4; i++) {
    //        printf("%s %d ",parr[i].name,parr[i].score);
    //    }
    //    int (*p)(int ,int ) = NULL;
    //    p = sum;
    //    printf("%d ",getValue(3,5,p));//虽然说回调函数的形式相对比较复杂,但起到了很好的代码保护作用 或留出了代码接口
        //void (*p4)() = NULL;//222222*//初始化一个指针函数
    //    printf("%d ",max(5, 6));
    //    p = max;//main函数外定义了一个函数指针
    //    //函数名就是地址 咱们把函数名赋值给定义好的函数指针
    //    p(3,5);//函数指针 其实它是为同一类函数使用而定义的
    //    p4 = printHello;
    //    p4();
    //    p = sum;
    //    printf("请输入函数名:");
    //    char temp[20] = {0};
    //    scanf("%s",temp);
    //    int (*p)(int,int) = NULL;
    //    if (strcmp(temp, "max") == 0) {
    //        p = max;
    //        printf("%d ",p(3,5));
    //    }
    //    else if (strcmp(temp, "sum") == 0) {
    //        p = sum;
    //        printf("%d ",p(3,5));
    //    }
    //    else
    //        printf("error!");
  • 相关阅读:
    python反爬之js混淆-字符串映射
    How to Provide a Default Trace for a Cloud Application
    Reset Peak Used Memory
    SAP 课程
    note 1639578 and 1622837
    SAP License error
    SAP BASIS-System Move
    初识Agile(敏捷项目管理)
    SAP HANA升级
    SAP FIORI 部署
  • 原文地址:https://www.cnblogs.com/sharkHZ/p/4984214.html
Copyright © 2020-2023  润新知