#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
/*
typedef 可以给函数指针类型取一个别名
typedef 没有定义新的类型、仅仅是给已经有的类型取一个别名、减少我们的坑爹输入
*/
int add(int a, int b){
return a + b;
}
//函数指针的定义方法:int (*pAdd)(int a, int b) 挖空函数名换成(*xxx)
//函数指针的类型:int (*)(int a, int b)
void msg(char * str){
MessageBoxA(0, str, str, 0);
}
void main1(){
int(*pAdd)(int a, int b);//定义函数指针
pAdd = add; // 初始化
printf("\n %d ", pAdd(3, 3));// 调用
void(*pMsg)(char * str) = msg; // 定义并且初始化
pMsg("nihao zhongguo ");
getchar();
}
typedef int (*pAdd)(int a, int b);
typedef void (*pMsg)(char * str);
void main(){
pAdd add1 = add;
printf("\n %d ", add1(3, 3));
pMsg msg1 = msg;
msg1("你好 天朝!");
getchar();
}
使用 typedef 简化函数指针
最新推荐文章于 2022-04-30 18:50:51 发布