联盟以struct错误 [英] Union in Struct Error

查看:173
本文介绍了联盟以struct错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

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

当初始化一个指针 *˚F,它指向 1型和做类似的实例:
f.element-> e 甚至只是 f.element ,我得到:

When initialising a pointer *f that points to an instance of type1 and doing something like: f.element->e or even just f.element, I get:

error: request for member ‘element’ in something not a structure or union

我是什么监督在这里?

What am I overseeing here?

推荐答案

元素是工会的名称,成员的不是名称 1型。你必须给联合元素名称:

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

这篇关于联盟以struct错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆