分享

结构体函数指针成员与函数指针类型的结构体类型形参的相互嵌套定义及使用问题

 华灯初放l 2020-03-06

@2018-10-24

结构体函数指针成员与函数指针类型的结构体类型形参的相互嵌套定义及使用问题

具体代码

复制代码
 1 #include <stdio.h>
 2 
 3 #define METHOD            0
 4 
 5 
 6 #if METHOD
 7 
 8 typedef void(*pf)(struct _struct *parameter);
 9 
10 #else
11 /* VC++6.0此法报错,gcc编译OK */
12 struct _struct;
13 typedef void(*pf)(struct _struct parameter);
14 
15 #endif
16 
17 
18 struct _struct
19 {
20     int i;
21     pf fun;
22 };
23 
24 #if METHOD
25 
26 void testFun(struct _struct *parameter)
27 {
28     printf("这是一个函数指针与结构体定义的先后问题!\n");
29     printf("testObj.i = %d\n", parameter->i);
30 }
31 
32 #else
33 
34 void testFun(struct _struct parameter)
35 {
36     printf("这是一个函数指针与结构体定义的先后问题!\n");
37     printf("testObj.i = %d\n", parameter.i);
38 }
39 
40 #endif
41 
42 
43 int main()
44 {
45     struct _struct testObj;
46 
47     testObj.i = 99;
48     testObj.fun = testFun;
49 
50 #if METHOD
51     testObj.fun(&testObj);
52 #else
53     testObj.fun(testObj);
54 #endif
55 }
复制代码

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多