联盟没有成员名字

问题描述 投票:0回答:1

我有一个内部结构的联合。当试图在 Linux 上编译时,我得到了一个相同类型的错误(或更多类似 50)。相同的代码适用于 Windows 上的 visual studio。

main.c:772:43: error: ‘union data’ has no member named ‘B’
fwrite(&u.B, sizeof(char), sizeof(u.B), out);
^

我在头文件 typedefs.h 中声明我所有的结构

union data {
    short buf;
    struct D {
        char A;
        char B;
    };
};

可能是什么问题和可能的解决方法?

c struct compiler-errors union
1个回答
0
投票

你在联合中声明了一个结构类型,但不是结构的实例。如果从结构中删除标签名称,您将在联合内定义一个匿名结构的实例。这也将使结构成员看起来像工会成员。

union data {
    short buf;
    struct {
        char A;
        char B;
    };
};
© www.soinside.com 2019 - 2024. All rights reserved.