如何初始化C联合结构成员(使用C89标准XC32 Compiler2.5)?
我有这个联合 /结构,
#define HOT_INI_DATA_SIZE 14
typedef union
{
struct
{
uint8_t OVER_LOAD_TEMP_BYTE_LOW;
uint8_t OVER_LOAD_TEMP_BYTE_HI;
uint8_t TEMP_CORRECTION_BYTE_LOW;
uint8_t TEMP_CORRECTION_BYTE_HI;
uint8_t STEAM_CORRECTION_BYTE;
uint8_t LEVEL_SENSOR_CALIBR_DATA[HOT_INI_DATA_SIZE - 5];
} fields;
uint8_t bytes[HOT_INI_DATA_SIZE];
} hot_cal_data_t;
我试图用狂热的方法使它变质,但没有运气:
const hot_cal_data_t initialisationHotData =
{
{0xD4},
{3},
{0},
{0},
{0},
{16, 16, 16, 16, 16, 16, 16, 16, 16 }
};
它抱怨初始化器周围额外的牙套,但是当我删除它时,错误会改变。 当我尝试较新的C99方法时:
{
{.fields.OVER_LOAD_TEMP_BYTE_LOW = 0xD4},
{.fields.OVER_LOAD_TEMP_BYTE_HI = 0x00},
...
};
它告诉我.fields未被识别。因此,我必须假设它使用C98方法仅初始化第一个联合元素。
任何人都可以告诉我正确的语法以获取此初始化。
I have this union / structure
#define HOT_INI_DATA_SIZE 14
typedef union
{
struct
{
uint8_t OVER_LOAD_TEMP_BYTE_LOW;
uint8_t OVER_LOAD_TEMP_BYTE_HI;
uint8_t TEMP_CORRECTION_BYTE_LOW;
uint8_t TEMP_CORRECTION_BYTE_HI;
uint8_t STEAM_CORRECTION_BYTE;
uint8_t LEVEL_SENSOR_CALIBR_DATA[HOT_INI_DATA_SIZE - 5];
} fields;
uint8_t bytes[HOT_INI_DATA_SIZE];
} hot_cal_data_t;
I am trying to initalise it with carious methods but have no luck:
const hot_cal_data_t initialisationHotData =
{
{0xD4},
{3},
{0},
{0},
{0},
{16, 16, 16, 16, 16, 16, 16, 16, 16 }
};
It is complaining about extra braces around initializer but when I remove it the error changes.
When I try the newer C99 method:
{
{.fields.OVER_LOAD_TEMP_BYTE_LOW = 0xD4},
{.fields.OVER_LOAD_TEMP_BYTE_HI = 0x00},
...
};
it tells me the .fields is not recognized. so I have to assume that it uses the C98 method of only initializing the first union element.
Can anyone please tell me the correct syntax to get this initialized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从那以后,我已经弄清楚了。
它使用C89标准,要求此语法初始化:
I have since figured it out.
It is using C89 standard and requires this syntax to initialize: