函数指针的用法。

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

typedef int (*funtcb)(void);

int functa(void)
{
printf("%s %d \n",func,LINE);
return 0;
}

int add(int a, int b )
{
int sum;
typeof(sum) t;
t = sum = a+b;
printf("[%s %d] sum %d \n",func,LINE,a+b);
return a+b;
}
int main(void)
{
{
funtcb p = functa;
p();
}
{
funtcb p2 = functa;
(p2)();
}
{
funtcb pp = &functa;
pp();
}
{
funtcb p3 = &functa;
(
p3)();
}
{
void pv = NULL;
pv = functa;
((funtcb)pv)();
}
{
void
pv2 = NULL;
pv2 = add;
(( int ()(int, int) )pv2)(3,3);
}
//typeof(一个函数的类型。
{
void
pv2 = NULL;
pv2 = add;
(( typeof(&add) )pv2)(3,3);
}
return 0;
}

猜你喜欢

转载自blog.51cto.com/yaxinsn/2475071
今日推荐