使用 typedef 简化函数指针

来源:互联网 发布:反网络尖兵 编辑:程序博客网 时间:2024/05/05 19:17
#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();}

0 0
原创粉丝点击