您的位置:首页 > 其它

使用 typedef 简化函数指针

2015-04-24 23:55 239 查看
#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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: