函数指针的作用

函数指针的主要作用,就是用来选择不同的调度函数,来满足特殊需求。它的优点,使程序设计更加灵活。缺点:初学者很难理解函数指针,从而引起程序的可读性不高。

1、使用函数指针选择调度函数

复制代码
#include "stm32f10x.h"
#include "USART1.h"
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()

void Function1(void)
{
	printf("Function1 work\r\n");
}

void Function2(void)
{
	printf("Function2 work\r\n");
}

void Function3(void)
{
	printf("Function3 work\r\n");
}

void (*FunctionPointer)(void);
//定义一个"函数指针",用来调用传入"void参数"的函数,该函数的返回值为void

int main(void)
{
	int cnt;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	USART1_Serial_Interface_Enable(115200);
	printf("\r\nCPU reset\r\n");

	cnt=0;
	while(1)
	{
		cnt++;
		if(cnt>3) cnt=1;
		if(cnt==1) FunctionPointer=Function1;
		//给函数指针赋值,则该函数指针会指向Function1()
		if(cnt==2) FunctionPointer=Function2;
		//给函数指针赋值,则该函数指针会指向Function2()
		if(cnt==3) FunctionPointer=Function3;
		//给函数指针赋值,则该函数指针会指向Function3()
		FunctionPointer();
	}
}

测试结果:

2、使用函数指针数组选择调度函数

复制代码
#include "stm32f10x.h"
#include "USART1.h"
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()

void Function1(void)
{
	printf("Function1 work\r\n");
}

void Function2(void)
{
	printf("Function2 work\r\n");
}

void Function3(void)
{
	printf("Function3 work\r\n");
}

void (*FunctionPointerArray[])(void) = { Function1, Function2,Function3 };
//定义一个"函数指针数组",用来调用传入"void参数"的函数,该函数的返回值为void

int main(void)
{
	int cnt;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	USART1_Serial_Interface_Enable(115200);
	printf("\r\nCPU reset\r\n");

	cnt=0;
	while(1)
	{
		FunctionPointerArray[cnt]();
		cnt++;
		if(cnt>2) cnt=0;
	}
}

测试结果:

3、将函数指针用作函数的传递参数选择调度函数

复制代码
#include "stm32f10x.h"
#include "USART1.h"
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()

void Function(int value)
{
	printf("value=%u\r\n",value);
}

//FunctionPointer是"函数指针",用来调用传入"void参数"的函数,该函数的返回值为void
//将"函数指针"用作传递参数,用来调用不同的调度函数
void callbackFunction(int value, void (*FunctionPointer)(int))
{
	FunctionPointer(value);
}

int main(void)
{
	int cnt;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	USART1_Serial_Interface_Enable(115200);
	printf("\r\nCPU reset\r\n");

	cnt=0;
	while(1)
	{
		callbackFunction(cnt,Function);
		cnt++;
		if(cnt>2) cnt=0;
	}
}

4、函数指针和指针函数的区别

函数指针,首先它是一个指针,不是函数;

指针函数首先是一个函数,只是它的返回值是一个指针。

函数指针声明格式:类型标识符 (*函数名) (参数)

void (*FunctionPointer)(void);
//定义一个"函数指针",用来调用传入"void参数"的函数,该函数的返回值为void

指针函数的声明格式为:类型标识符 *函数名(参数表);

char array[]="123456\r\n";
char * PointerFunction(char *buf) //这是指针函数

{

buf++;

return(buf);

}

void Test_PointerFunction(void)

{

char *p;

p=PointerFunction(array);

printf("*p=%s",p);

}

复制代码
#include "stm32f10x.h"
#include "USART1.h"
#include "stdio.h"  //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf()

/*
函数指针,首先它是一个指针,不是函数;
指针函数首先是一个函数,只是它的返回值是一个指针。

函数指针声明格式:类型标识符 (*函数名) (参数)
指针函数的声明格式为:类型标识符 *函数名(参数表);
*/
void Function(int value)
{
	printf("value=%u\r\n",value);
}

char array[]="123456\r\n";
char * PointerFunction(char *buf)
{
	buf++;
	return(buf);
}

void Test_PointerFunction(void)
{
	char *p;
	p=PointerFunction(array);
	printf("*p=%s",p);
}

//FunctionPointer是"函数指针",用来调用传入"void参数"的函数,该函数的返回值为void
//将"函数指针"用作传递参数,用来调用不同的调度函数
void callbackFunction(int value, void (*FunctionPointer)(int))
{
	FunctionPointer(value);
}

int main(void)
{
	int cnt;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
	USART1_Serial_Interface_Enable(115200);
	printf("\r\nCPU reset\r\n");
	Test_PointerFunction();

	cnt=0;
	while(1)
	{
		callbackFunction(cnt,Function);
		cnt++;
		if(cnt>2) cnt=0;
	}
}
相关推荐
森焱森1 小时前
60 美元玩转 Li-Fi —— 开源 OpenVLC 平台入门(附 BeagleBone Black 驱动简单解析)
c语言·单片机·算法·架构·开源
千帐灯无此声1 小时前
Linux 测开:日志分析 + 定位 Bug
linux·c语言·c++·bug
骁的小小站2 小时前
HDLBits刷题笔记和一些拓展知识(十一)
开发语言·经验分享·笔记·其他·fpga开发
ChuHsiang2 小时前
【C语言学习】实现游戏——三子棋
c语言
L_autinue_Star5 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
怀旧,5 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
凤年徐7 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
学废了wuwu7 小时前
深度学习归一化方法维度参数详解(C/H/W/D完全解析)
c语言·人工智能·深度学习
无小道7 小时前
c++--typedef和#define的用法及区别
c语言·开发语言·汇编·c++
敲上瘾8 小时前
传输层协议UDP原理
linux·c语言·网络·网络协议·udp