码迷,mamicode.com
首页 > 编程语言 > 详细

C语言----函数指针

时间:2015-06-17 21:49:02      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:回调   参数   c语言   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;
}

C语言----函数指针

标签:回调   参数   c语言   c   函数指针   

原文地址:http://blog.csdn.net/zhengang007/article/details/46535463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!