哪个是初始化 union/struct 的正确方法?
Which is the correct way to initialize a union/struct?
我有以下联合:
typedef union
{
struct
{
uint8_t LSB;
uint8_t MSB;
};
int16_t complete;
}uint16ByteT;
知道我要使用我的类型并初始化变量。扫描SO(我认为)后我找到了解决方案:
uint16ByteT myVariable = {0};
但是我的编译器给我一条错误信息:
simple type required for "@"
通常 xc8 编译器使用“@”在特定地址引入变量。
要初始化匿名 struct
/union
您可以使用:
uint16ByteT myVariable = {{0}, .complete = 0};
或者干脆
uint16ByteT myVariable = {{0}};
注意 uint16ByteT
而不是 uint16Byte
另请注意,由于此版本引入了匿名struct
s/union
s,因此需要在C11模式下编译。
我有以下联合:
typedef union
{
struct
{
uint8_t LSB;
uint8_t MSB;
};
int16_t complete;
}uint16ByteT;
知道我要使用我的类型并初始化变量。扫描SO(我认为)后我找到了解决方案:
uint16ByteT myVariable = {0};
但是我的编译器给我一条错误信息:
simple type required for "@"
通常 xc8 编译器使用“@”在特定地址引入变量。
要初始化匿名 struct
/union
您可以使用:
uint16ByteT myVariable = {{0}, .complete = 0};
或者干脆
uint16ByteT myVariable = {{0}};
注意 uint16ByteT
而不是 uint16Byte
另请注意,由于此版本引入了匿名struct
s/union
s,因此需要在C11模式下编译。