我有以下结构:
struct type1 {
struct type2 *node;
union element {
struct type3 *e;
int val;
};
};
初始化指向 type1
实例的指针 *f
并执行以下操作时:
f.element->e
甚至只是 f.element
,我得到:
error: request for member ‘element’ in something not a structure or union
我在这里监督什么?
最佳答案
element
是 union 的名称,而不是 type1
成员的名称。您必须为 union 元素
指定一个名称:
struct type1 {
struct type2 *node;
union element {
struct type3 *e;
int val;
} x;
};
然后您可以通过以下方式访问它:
struct type1 *f;
f->x.e
关于c - 结构错误中的 union ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037832/