结构体中函数指针初始化

/**
 * 为结构体中的指针数组赋值
 */

#include <stdio.h>

typedef struct test
{
    void (*p)(void);
    void (*q)(void);
    void (*y)(void);
}test;

void f1(void)
{
    printf("f1\n");
}

void f2(void)
{
    printf("f2\n");
}

void f3(void)
{
    printf("f3\n");
}

int main(void)
{
    test aa = {
        p : f1, //方法1
        .q = f2, //方法2, 一般这种方式在全局变量初始化的时候常用
    };
    aa.y = f3; //方法3

    aa.p();
    aa.q();
    aa.y();

    return 0;
}

猜你喜欢

转载自blog.csdn.net/huan447882949/article/details/81784218