gpt4 book ai didi

c - 初始化 union/结构的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 14:47:30 25 4
gpt4 key购买 nike

我有以下 union :

typedef union
{
struct
{
uint8_t LSB;
uint8_t MSB;
};
int16_t complete;
}uint16ByteT;

知道我想使用我的类型并初始化变量。扫描后(我想)我找到了解决方案:

uint16ByteT   myVariable  = {0};

但是我的编译器给了我一条错误消息:

simple type required for "@"

通常xc8编译器使用“@”将变量带到特定地址。

最佳答案

要初始化匿名struct/union,您可以使用:

uint16ByteT myVariable = {{0}, .complete = 0};

或者简单地

uint16ByteT myVariable = {{0}};

注意uint16ByteT而不是uint16Byte

另请注意,您需要在 C11 模式下进行编译,因为此版本中引入了匿名 structs/unions。

关于c - 初始化 union/结构的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51172200/

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