您的位置:首页 > 其它

为结构体中函数指针赋值的两种方法

2018-01-31 10:53 253 查看
/**
02. * 为结构体中的指针数组赋值
03. */
04.
05.#include <stdio.h>
06.
07.typedef struct test
08.{
09.    void (*p)(void);
10.    void (*q)(void);
11.    void (*y)(void);
12.}test;
13.
14.void f1(void)
15.{
16.    printf("f1\n");
17.}
18.
19.void f2(void)
20.{
21.    printf("f2\n");
22.}
23.
24.void f3(void)
25.{
26.    printf("f3\n");
27.}
28.
29.int main(void)
30.{
31.    test aa = {
32.        p : f1, //方法1
33.        .q = f2, //方法2, 一般这种方式在全局变量初始化的时候常用
34.    };
35.    aa.y = f3; //方法3
36.
37.    aa.p();
38.    aa.q();
39.    aa.y();
40.
41.    return 0;
42.}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: