简体   繁体   English

如何在C中访问union中的struct成员?

[英]How to access a struct member inside a union in C?

I have the following union: 我有以下联盟:

union employee
{
    char key;

    struct manager
    {
        short int age;
        float shares;
        short int level;
    };

    struct worker
    {
        short int age;
        short int skill;
        short int department;
    };

} company[10];

How can I access a member of a structure which is inside the union employee ? 如何访问union employee内部的结构成员? I tried to access the age member of the manager structure this way: 我尝试以这种方式访问manager结构的age成员:

company[i].manager.age

But I get error C2039: 'manager' : is not a member of 'employee' . 但我收到error C2039: 'manager' : is not a member of 'employee'

Add something after the tag declaration. 在标记声明后添加一些内容。 Perhaps: 也许:

struct manager
{
    short int age;
    float shares;
    short int level;
} manager;

Side note: you're not using the union right. 旁注:你没有正确使用工会。 The key, ie the field that tells you whether you are dealing with a manager or with a mere worker should be in an enclosing object, outside the union . 关键,即告诉您是在与经理或仅与工人打交道的领域,应该在工会之外的附件中 Perhaps: 也许:

struct employee {
    char key;

    union {
        struct manager ...;
        struct worker ...;
    } u;
};

As dasblinkenlight notes, you could declare your manager / worker tags outside the union. 正如dasblinkenlight所说,你可以在联盟之外声明你的经理/工人标签。

Anonymous structs/unions is not part of the C standard, but rather a not very widespread GNU extension. 匿名结构/联合不是C标准的一部分,而是一个不太广泛的GNU扩展。

In your particular example some compilers (mainly GCC) will allow you to access manager and worker unique variables via eg company[i].shares or company[i].department , but company[i].age is ambiguous and the compiler will not know which one is meant. 在您的特定示例中,一些编译器(主要是GCC)将允许您通过例如company[i].sharescompany[i].department访问manager和worker唯一变量,但company[i].age是不明确的,编译器不会知道哪一个是意思。 Your approach is similar to trying to define 您的方法类似于尝试定义

 union {
      int num;
      float num;
 } union_number;

which is not even valid C. 这甚至不是有效的C.

there are two ways to solve this. 有两种方法可以解决这个问题。

a) moving the shared attributes outside the struct (the evil GNU way, please don't do that, I know for a fact that icc does not even compile this) a)移动结构外的共享属性(邪恶的GNU方式,请不要这样做,我知道icc甚至不编译这个事实)

union employee
{
    char key;

    struct person {
        short int age;

        union {
            struct manager
            {
                float shares;
                short int level;
            };

            struct worker
            {
                short int skill;
                short int department;
            };
        }
    };

} company[10];

b) or the more clean standardized way to name your structs: b)或更简洁的标准方式来命名你的结构:

union employee
{
    char key;

    struct manager
    {
        short int age;
        float shares;
        short int level;
    } manager;

    struct worker
    {
        short int age;
        short int skill;
        short int department;
    } worker;

} company[10];

in this case you will be able to access the struct elements via company[i].manager.age , company[i].worker.skill and so on. 在这种情况下,您将能够通过company[i].manager.agecompany[i].worker.skill等访问struct元素。

Please pay attention that at runtime there is no way to test whether your union contains a key, a manager or a worker. 请注意,在运行时,无法测试您的联合是否包含密钥,经理或工作人员。 That must be known in advance. 必须提前知道。

Another thing: I am not sure if this is intended, but in your declaration you cannot save a key together with a manager or a worker. 另一件事:我不确定这是否有意,但在您的声明中,您无法与经理或工人一起保存密钥。 Your union contains only one of key, manager or worker 你的工会只包含的关键,经理或工人一个

this error is because you are trying to access the structure elements by the name of the structure itself, which is not valid. 此错误是因为您尝试通过结构本身的名称访问结构元素,这是无效的。 In order to access the elements of a structure, one should first create an object to it. 为了访问结构的元素,首先应该为它创建一个对象。

in the following code manage and work are the objects of the structure manager and worker respectively. 在下面的代码中,manage和work分别是结构管理器和worker的对象。

union employee { char key; 工会员工{char key;

struct manager
{
    short int age;
    float shares;
    short int level;
}manage;

struct worker
{
    short int age;
    short int skill;
    short int department;
}work;

} company[10]; 公司[10];

now to access the member u can use "company[i].manage.age" 现在要访问该成员你可以使用“company [i] .manage.age”

You can access the members, the way you are trying( but the structures which are inside the union should have unique members) but you should make sure that while compiling the code gcc should know that you are trying like that, 您可以按照您尝试的方式访问成员(但是联合内部的结构应该具有唯一成员)但是您应该确保在编译代码时gcc应该知道您正在尝试这样做,

command : gcc -fms-extensions file_name.c which supports from c11. 命令: gcc -fms-extensions file_name.c ,支持c11。

like : 喜欢 :

union employee
{
char key;

struct manager
{
    short int age;
    float shares;
    short int level;
};

struct worker
{
    short int age;
    short int skill;
    short int department;
};
} company[10];

This basically gives compilation error because it finds ambiguity between the members of manager and worker. 这基本上会产生编译错误,因为它会发现管理员和工作人员之间存在歧义。

So if you change your code to: 因此,如果您将代码更改为:

union employee
{
char key;

struct manager
{
    short int age;
    float shares;
    short int level;
};

struct worker
{
    short int age1;
    short int skill;
    short int department;
};

} company[10];

or else you can keep directly under union. 否则你可以直接保持联盟。

unless you don't specify -fms-extensions while compiling it will give compile time errors. 除非在编译时没有指定-fms-extensions ,否则会产生编译时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM