C11-函数指针

本文详细介绍了C11中函数指针的使用,包括函数指针的定义、函数回调、动态排序以及函数返回值为函数指针的情况。通过示例展示了如何使用函数指针进行操作,如计算最大值、打印函数、动态调整结构体数组等,并讨论了结构体和函数指针的结合使用,用于实现动态排序和根据分数添加特定名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、函数指针定义
二、函数回调
三、动态排序
四、函数返回值是函数指针

代码:
int maxValue(int a, int b)
{
return a > b ? a : b;
}

void printHello(void)
{
printf(“hello !\n”);
}

int sum(int a, int b)
{
return a + b;
}

// 给此函数指针类型起名为FN

typedef int (*FN)(int a, int b);

typedef void (*VO)(void);

int getVal(int a, int b, FN p)
{
return p(a, b);
}
typedef struct student {
char name[20];
int num;
float score;
} Student;

//typedef void (*v)(Student *stu, int count);
//
//void getValu(Student *stu, int count, v q)
//{
// for (int i = 0; i < count; i++) {
// if (stu[i].score >= 90) {
// printf(“%s高富帅\n”, stu[i].name);
// }
// }
//}

// 回调函数!!!
// 女 + 白富美

void catNameM(char *p)
{
strcat(p, “高富帅”);
}
// 男 + 高富帅
void catNameF(char *p)
{
strcat(p, “白富美”);
}
typedef void (*C)(char *p);
// 分数大于90,名字后加 catname
void addName(Student *stu, int count, C name)
{
for (int i = 0; i < count; i++) {
if (stu[i].score >= 90) {
name(stu[i].name);
}
}
}
// 打印
void printName(Student *stu, int count)
{
for (int i = 0; i < count ; i++) {
printf(“%s %d %.2f\n”, stu[i].name, stu[i].num, stu[i].score);
}
}

typedef struct student1 {
char name[20];
int num;
float score;
} Student1;
void arrNum(Student1 *stu, int count)
{
//for (int i; i < count; i++) {
while (stu[count].num > stu[count - 1].num) {
if (count < 0) {
break;
}
Student1 temp = stu[count];
stu[count] = stu[count - 1];
stu[count - 1] = temp;
}
//}
}
void arrName(Student1 *stu, int count)
{
//for (int i = 0; i < count; i++) {
while (strcmp(stu[count].name, stu[count - 1].name) > 0) {
if (count < 0) {
break;
}
Student1 temp = stu[count];
stu[count] = stu[count - 1];
stu[count - 1] = temp;
}
//}
}
void arrScore(Student1 *stu, int count)
{
//for (int i = 0; i < count; i++) {
while (stu[count].score > stu[count - 1].score) {
if (count < 0) {
break;
}
Student1 temp = stu[count];
stu[count] = stu[count + 1];
stu[count + 1] = temp;
}
//}
}
typedef void (*arr)(Student1 *stu, int count);
void arrStudent(Student1 *stu, int count, arr z)
{
char a[10] = {0};
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
scanf(“%s”, a);
}
}
}

int opeSum(int a, int b)
{
return a + b;
}

int opeSub(int a, int b)
{
return a - b;
}
int opeMul(int a, int b)
{
return a * b;
}
int opeDiv(int a, int b)
{
return a / b;
}
typedef int (*ariOpe)(int a, int b);
int getValu(int a, int b, ariOpe ao)
{

return ao(a, b);

}

//void ariOpe(, )
//{
//
//}

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

// int ret = p(3, 5);
// printf(“%d\n”, ret);

// 定义一个函数指针
// 函数指针->即函数类型的指针
// 定义函数指针需先有函数的定义

// FN p = NULL;
// p = maxValue;
// int retP = p(3, 5);
// printf(“%d\n”, retP);

// VO p = NULL;
// p = printHello;
// p();

// 练习2.

// char fun[10] = {0};
// //int (*p)(int a, int b) = NULL;
// FN p = NULL;
// scanf(“%s”, fun);
// // 判断
// if ((strcmp(fun, “max”) != 0) && (strcmp(fun, “sum”) != 0)) {
// // 报错
// printf(“error\n”);
// // 返回错误码
// return -1;
// }
// if (0 == strcmp(fun, “max”)) {
// p = maxValue;
// }
// if (0 == strcmp(fun, “sum”)) {
// p = sum;
// }
// // int ret = p(3, 5);
// int ret = getVal(3, 5, p);
// printf(“%d\n”, ret);

// Student stu[5] = {
// {“yi”, 1, 60},
// {“er”, 2, 70},
// {“san”, 3, 80},
// {“si”, 4, 90},
// {“wu”, 5, 100}
// };
// int count = sizeof(stu) / sizeof(stu[0]);
// addName(stu, count, catNameM);
// printName(stu, count);

// Student1 stu[5] = {
// {“yi”, 3, 58},
// {“er”, 4, 67},
// {“san”, 1, 45},
// {“si”, 2, 23},
// {“wu”, 5, 98}
// };
// int count = sizeof(stu) / sizeof(stu[0]);

char fun[20] = {0};
int a = 0, b = 0;
//int (*p)(int a, int b) = NULL;
ariOpe p = NULL;
printf("请输入(eg:sum 3 5):\n");
scanf("%s%d%d", fun, &a, &b);
// 判断
if ((strcmp(fun, "sum") != 0) && (strcmp(fun, "sub") != 0) && (strcmp(fun, "mul") != 0) && (strcmp(fun, "div") != 0)) {
    // 报错
    printf("error\n");
    // 返回错误码
    return -1;
}
if (0 == strcmp(fun, "sub")) {
    p = opeSub;
}
if (0 == strcmp(fun, "sum")) {
    p = opeSum;
}
if (0 == strcmp(fun, "mul")) {
    p = opeMul;
}
if (0 == strcmp(fun, "div")) {
    p = opeDiv;
}
// int ret = p(3, 5);
int ret = getVal(a, b, p);
printf("%d\n", ret);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值