C语言----函数指针

回调函数

1、 函数指针 做参数
这里写图片描述
2、 回调过程
这里写图片描述

例代码

//
//  main.m
//  C_Project_12
//
//  Created by  on 15/3/26.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <Foundation/Foundation.h>

//课堂练习题:写一函数查找成绩90分以上的学员,使⽤用回调函数在姓名后加”⾼高富 帅”。

//1.定义结构体类型
typedef struct student {
    char name[20];
    float score;
} Student;

Student *generateStudentsInfo(int count);
Student *generateStudentsInfo(int count) {
    Student *stus = malloc(sizeof(Student) * count);
    for (int i = 0; i < count; i++) {
        printf("请输入第%d个学生的信息\n", i + 1);
        printf("姓名:");
        scanf("%s", (stus + i)->name);
        printf("成绩:");
        scanf("%f", &(stus + i)->score);
    }
    return stus;
}

void printStudentsInfo(Student *stus, int count);
void printStudentsInfo(Student *stus, int count) {
    printf("\n---------------------------\n");
    for (int i = 0; i < count; i++) {
        printf("姓名:%s\t\t\t\t成绩:%.2f\n", (stus + i)->name, (stus + i)->score);
    }
    printf("\n---------------------------\n");
}

void modifyName(char *name);
void modifyName(char *name) {
    strcat(name, "-高富帅");
}

void searchStudentInfo(Student *stus, int count, float score, void (*point)(char *));
void searchStudentInfo(Student *stus, int count, float score, void (*point)(char *)) {
    for (int i = 0; i < count; i++) {
        if ((stus + i)->score > score) {
            point((stus + i)->name);
        }
    }
}


int main(int argc, const char * argv[]) {

    Student *stus = generateStudentsInfo(2);
    printStudentsInfo(stus, 2);
    searchStudentInfo(stus, 2, 90, modifyName);
    printStudentsInfo(stus, 2);

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值