我有一个像这样定义的联合:
typedef union {
enum {
REVISION = 0,
CURRENT_VERSION = REVISION
};
enum FLAGS{
FLAG_DEFAULT = 0x00000000,
FLAG_EOD = 0x00000001,
FLAG_OUTOFORDER = 0x00000002
};
CHAR _filler[32];
struct INTERNAL_STRUCTURE {
UINT16 type;
UINT16 flags;
};
}CORRHDR
Run Code Online (Sandbox Code Playgroud)
如何从我的代码访问INTERNAL_STRUCTURE的成员?
我以为我可以这样做:
CORRHDR hdr;
hdr.INTERNAL_STRUCTURE.type = 1;
Run Code Online (Sandbox Code Playgroud)
我错了.我可以看到联盟中的枚举,但没有别的.有人可以向我解释这种类型的结构(或好处)吗?
您已声明了所调用的类型INTERNAL_STRUCTURE,但未声明该类型的实际实例.试试这个:
typedef union {
//...
CHAR _filler[32];
struct {
UINT16 type;
UINT16 flags;
} INTERNAL_STRUCTURE;
}CORRHDR;
Run Code Online (Sandbox Code Playgroud)
然后访问该字段:
CORRHDR ch;
printf("%u\n", ch.INTERNAL_STRUCTURE.type);
Run Code Online (Sandbox Code Playgroud)