1. 程式人生 > >結構體裡面的函式指標怎麼初始化

結構體裡面的函式指標怎麼初始化


/**
 * 為結構體中的指標陣列賦值
 */
 
#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;
}