c - 结构错误中的 union

标签 c struct unions

我有以下结构:

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/

相关文章:

Mac OS X 上的自定义 igraph igraph_get_shortest_paths_dijkstra 函数

c - 当数组作为参数传递时会发生什么?

c - 如何让我的源等到按钮释放后再继续?

gcc - 编译错误: Redefinition of union name

c++ - 为什么这不是一个 constexpr?

c - 警告 : C4013 in C lang

c++ - 如何将数据从一个结构链接到另一个结构

c - 在 emacs 中设置自动完成模式以与 C 结构配合使用

c++ - 如何比较结构中不同索引的元素

c - 定义有条件存在的元素的结构