gpt4 book ai didi

c++ - 初始化结构 union

转载 作者:太空宇宙 更新时间:2023-11-04 01:01:32 25 4
gpt4 key购买 nike

struct EXMPL
{
union
{
struct
{
struct
{
uint8_t AA;
uint8_t BB;
uint8_t CC;
uint8_t DD;
}Rev;

struct
{
uint8_t XX;
uint8_t VV;
uint8_t WW;
uint8_t FF;
}IDs;
};

struct UNN
{
uint32_t A;
uint32_t B;
};
};
};

当我尝试像这样初始化它时:

EXMPL aStruct = {{ 0x00, 0x00, 0x01, 0x00 }, { 0x00, 0x00, 0x00, 0x00 }};

我收到“太多初始化值”错误。任何帮助将不胜感激,谢谢。

最佳答案

C 中,EXMPL aStruct = {{... 需要是 struct EXMPL aStruct = {{...

缺少 2 级 {{ - 添加了

int main(void) {
struct EXMPL // level 1
{
union // level 2
{
struct // level 3
{
struct // level 4
{
uint8_t AA;
uint8_t BB;
uint8_t CC;
uint8_t DD;
}Rev;

struct
{
uint8_t XX;
uint8_t VV;
uint8_t WW;
uint8_t FF;
}IDs;
} ;

// level 3
// but irrelevant to initialization as only 1st union member counts by default
struct UNN
{
uint32_t A;
uint32_t B;
} ;
};
};

struct EXMPL aStruct = {{{{ 0x00, 0x00, 0x01, 0x00 }, { 0x00, 0x00, 0x00, 0x00 }}}};
...

关于c++ - 初始化结构 union ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36924421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com