简体   繁体   中英

How to initialize C Union structure members (using C89 Standard XC32 compiler2.5)?

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.

I have since figured it out. It is using C89 standard and requires this syntax to initialize:

const hot_cal_data_t initialisationHotData =
{
    {
        0xD4,
        0x03,
        0,
        0,
        0,
        {
            16, //.LEVEL_SENSOR_CALIBR_DATA[0]
            16, //.LEVEL_SENSOR_CALIBR_DATA[1]
            16, //.LEVEL_SENSOR_CALIBR_DATA[2]
            16, //.LEVEL_SENSOR_CALIBR_DATA[3]
            16, //.LEVEL_SENSOR_CALIBR_DATA[4]
            16, //.LEVEL_SENSOR_CALIBR_DATA[5]
            16, //.LEVEL_SENSOR_CALIBR_DATA[6]
            16, //.LEVEL_SENSOR_CALIBR_DATA[7]
            16  //[8]
        }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM