#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedefstruct student //定义一个结构体存放学生信息
{
char name[20];
int score;
int age;
}Stu;
void pinjie(char name[]) //这是被调用的函数,被函数指针所指向的函数。它是一个工程的具体实现。
{
strcat(name, "三好学生");
}
void f1(Stu stu[],int n,void (*p)(char name[])) //结构体数组,数组长度,函数指针作为形参。(注意函数指针的定义,组成元素和目的)。
{
for (int i = 0; i < 5; i++) {
if (stu[i].score>80) {
p(stu[i].name); //调用函数(注意参数的传递)。
}
}
}
int main(int argc, constchar * argv[])
{
Stu stu[5] = {
{"zhangdsan",69,25},
{"lisi",86,26},
{"wangwu",67,21},
{"maliu",78,20},
{"tangqi",63,27}
};
f1(stu,5,pinjie); //调用实现函数(注意参数的传递和调用方式,如果函数是有返回值的就要用一个跟函数类型一样的变量来接收它)。
for (int i = 0; i < 5; i++) {
printf("%8s%8d%8d\n",stu[i].name,stu[i].score,stu[i].age);
}
return0;
}