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

来源:互联网 发布:合同矩阵的性质 编辑:程序博客网 时间:2024/06/05 04:03
/* *结构体中函数指针的用法。 */#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)() = addprintf("the result is %d\n", test.func(5, 3));}

原创粉丝点击