Union in Struct Error

前端 未结 2 567
刺人心
刺人心 2021-01-26 04:42

I have the following struct:

struct type1 {
    struct type2 *node;
    union element {
        struct type3 *e;
        int val;
    };
};

Whe

2条回答
  •  误落风尘
    2021-01-26 04:50

    element is the name of the union, not the name of a member of type1. You must give union element a name:

    struct type1 {
    struct type2 *node;
        union element {
            struct type3 *e;
            int val;
        } x;
    };
    

    then you can access it as:

    struct type1 *f;
    f->x.e
    

提交回复
热议问题