您的位置:首页 > 其它

结构体中函数指针的用法。

2012-05-13 14:29 288 查看
/*
*结构体中函数指针的用法。
*/
#include <stdio.h>

struct DEMO1
{
int (*func)(int, int);

};

static int add(int a, int b)
{
return (a + b);
}

int main(void)
{
int (*p)();
struct DEMO1 test;
test.func = add;          // add 为函数名,和数组一样,代表函数的首地址,是个常量。把add的地址传给func.

printf("the result is %d\n", (*test.func)(2, 3));//(*test.func)() = (test.func)() = add

printf("the result is %d\n", test.func(5, 3));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct