c语言怎么用数组存放函数指针,C语言-函数指针和函数指针数组的定义与使用

函数指针的定义方法:

1. 先定义出函数类型,再通过类型定义出函数指针

2.先定义出函数指针类型,再定义函数指针

3.直接定义函数指针变量

#include

#include

#include"config.h"

void func()

{

printf("hello world\n");

}

//方法1

void test01()

{

/*

int arr[5];

int(ARRAY)[5];//定义数组类型

int(*ARRAY)[5];//定义数组指针类型

*/

//先定义出函数类型,再通过类型定义出函数指针

typedef void(FUNC_TYPE)();

FUNC_TYPE* pFunc = func;

pFunc();

}

void test02()

{

//先定义函数指针类型,再定义函数指针

typedef void(*FUNC_TYPE)();

FUNC_TYPE pFunc = func;

pFunc();

}

void test03()

{

//直接定义函数指针变量

void(*pFunc)() = func;

pFunc();

}

int main()

{

//test01();

//test02();

test03();

return 0;

}

函数指针数组的定义与使用

#include

#include

#include"config.h"

void func1()

{

printf("func1的调用\n");

}

void func2()

{

printf("func2的调用\n");

}

void func3()

{

printf("func3的调用\n");

}

void test03()

{

int i;

//函数指针数组的定义

void(*pFunc[3])();

pFunc[0] = func1;

pFunc[1] = func2;

pFunc[2] = func3;

for(i = 0;i < 3;i++)

{

pFunc[i]();

}

}

int main()

{

//test01();

//test02();

test03();

return 0;

}

输出结果:

func1的调用

func2的调用

func3的调用

请按任意键继续. . .

496a2724c91f3df4839ed92d5e2a59b0.png

4a668feaa3018a2e1706f07e5002bcc8.png

虎子敲可爱(๑• . •๑)

发布了67 篇原创文章 · 获赞 11 · 访问量 3876

私信

关注

标签:定义,pFunc,int,void,C语言,数组,函数指针,include

来源: https://blog.csdn.net/weixin_42596333/article/details/104591597

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值