Can I 'extend' a struct in C?

后端 未结 7 1486
太阳男子
太阳男子 2020-12-08 16:05
typedef struct foo_s {
    int a;
} foo;

typedef struct bar_s {
    foo;
    int b;
} bar;

Essentially I want to do:

bar b;
b.a;
<         


        
7条回答
  •  青春惊慌失措
    2020-12-08 16:50

    You can try using inheritance:

    struct foo_s
    {
        int a;
    };
    
    struct bar_s: foo_a
    {
        int b;
    };
    

    Works in C++, not sure if it works in C.

提交回复
热议问题