函数指针数组

什么函数指针数组:❓

数组是一个存放相同类型数据的存储空间📃
指针数组内的元素值表示的是地址🏠
函数指针数组,简单来说,就是数组的每个元素存放的是函数的入口地址(函数的名字

怎么存放函数指针?

1.先明白怎么定义,使用typedef

typedef声明函数指针👉

typedef int (*Test)(int a,int b);//声明函数类型

int add(int a,int b){
	 printf("%d\n",a+b);
	return a+b;
}
int mul(int a,int b){
	 printf("%d\n",a*b);
	return a*b;
}
Test FUN_list[] = { //定义一个数组,里面全是Test类型的函数指针(函数名,入口地址)
	add,
	mul
};

注❕:数组存放相同类型数据,所以内部的元素都要是int (*Test)(int a,int b)的,在调用某一函数时,直接具体到数组没某一元素值即可

怎么使用?

1.第一种:直接找到定义好的函数入口,然后调用即可

typedef int (*Test)(int a,int b);//声明函数类型

int add(int a,int b){
	 printf("%d\n",a+b);
	return a+b;
}
int mul(int a,int b){
	 printf("%d\n",a*b);
	return a*b;
}
Test FUN_list[] = { //定义一个数组,里面全是Test类型的函数指针(函数名,入口地址)
	add,
	mul
int main()
{
   /*  Write C code in this online editor and run it. */
   printf("Hello, World! \n");

    int choice;
    printf(" input:");
    scanf("%d",&choice);
	int para1 = 1;
	int para2 = 2;
    FUN_list[choice](para1,para2); //像普通函数的调用方式即可,传入函数参数
	for(int i = 0;i<2;i++){
			FUN_list[i](para1,para2);
	}
   return 0;
}

在这里插入图片描述

2.第二种:用void*类型的数组存放函数指针,在使用的时候强制转换类型,即可找到函数入口并调用

  void 类型比较特殊,它可以表示所有的类型,但是又不能像其他类型那样声明成实体(void a;❌)。在很多项目中的函数的参数使用void* 类型数组,在void* 数组中,即可以有int型,可以有char型,也可以有结构体,将这些参数放在一个void* 型数组中。
  使用void *二元数组存放提前定义好的函数入口,因为void 可以表示所有的类型,所以在使用时需要 **强制转换为需要使用的类型,无论是变量还是函数。👇 **

#define MAXNUM 3
typedef void (*FUN)(void *para); 
enum{
	ZEOR = 0,
    ONE = 1,
    TOW = 2,
    THREE = 3
};
void add(void *para){
	 printf("This is add para is %d\n",*(int *)para);
	return;
}
void mul(void *para){
	 printf("This is mul para is %d\n",*(int *)para);
	return;
}
void *FUN_List[MAXNUM][2] = {
    {ZEOR, NULL},
    {ONE, add},
    {TOW, mul},
};

在使用的时候,提前定义一个函数指针变量用来存放转换类型的后函数入口,然后再调用函数

int main()
{
   /*  Write C code in this online editor and run it. */
   printf("Hello, World! \n");
    int choice;
    printf(" input:");
    scanf("%d",&choice);
	int para1 = 1;
	int para2 = 2;
	for(int i = 0;i<MAXNUM;i++){
		if(choice == FUN_List[i][0]){ //FUN_List[i][0]的值表示一个地址的值,只是对内部的值做一个比较,并没有具体到使用数据类型,不用做转换
		//若是int *a = (int *)FUN_List[i][0];这样就要做类型转换了
			 FUN fun = (FUN)FUN_List[i][1];  //将void指针类型强制转换为函数指针类型
		  		if(fun){
		      		fun((void *)&para1); //调用该函数
		  		}  
		}
	}
   return 0;
}

除了使用枚举类型,也可以直接使用字符串:

void *FUN_List[MAXNUM][2] = {
    {"start", NULL},
    {"closeLED", add},
    {"openLED", mul},
};

int main()
{
   /*  Write C code in this online editor and run it. */
   printf("Hello, World! \n");
    int choice;
    printf(" input:");
    scanf("%d",&choice);
	int para1 = 1;
	int para2 = 2;
	for(int i = 0;i<MAXNUM;i++){
		/*
		if(choice == FUN_List[i][0]){
			 FUN fun = (FUN)FUN_List[i][1];
		  		if(fun){
		      		fun((void *)&para1);
		  		}  
		}*/
		if(memcmp((char *)FUN_List[i][0],"openLED",sizeof("openLED")) == 0){
			 FUN fun = (FUN)FUN_List[i][1];
		  		if(fun){
		      		fun((void *)&para1);
		  		} 
		}
	}
   return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想和我重名?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值