C 语言中的指针函数写法
1. 指针函数使用
#include <stdio.h>
#define uint8 unsigned char
#define uint16 unsigned short
#define uint32 unsigned int
uint8 get_device_type_flash() {
printf("--------------- run ------------------ \n");
return 100;
}
uint8 get_flash_1byte(void * funtype){
uint8 (* fun)() = funtype;
uint8 num = fun();
return num;
}
uint8 get_device_type() { return get_flash_1byte(get_device_type_flash); }
int main(int argc, char const *argv[])
{
uint8 num = get_device_type();
printf("res: %d \n", num);
return 0;
}
2. 指针函数组
#include <stdio.h>
void a1(int a, void * ptr) {
printf("[fun a1] %02x %p %p \n", a, a, ptr);
}
void a2(int a, void * ptr) {
printf("[fun a2] %02x %p %p \n", a, a, ptr);
}
// ****************数组存储指针函数的实现****************/
int main(int argc, char const *argv[])
{
int a = 4;
unsigned char *ptr = "hanguangxue";
printf("%02x %p %p \n", a, a, ptr);
printf("a1 a2 方法名地址: %p %p \n", a1, a2);
void (*fun[2])(int, void *) = {a1, a2};
printf("%p \n", fun[0]);
printf("%p \n", fun[1]);
fun[0](a, ptr);
fun[1](a, ptr);
return 0;
}
本文来自博客园踩坑狭,作者:韩若明瞳,转载请注明原文链接:https://www.cnblogs.com/han-guang-xue/p/14953187.html