Compiler: Bug of feature (struct/union initialization)

Hello,

I observed a strange behaviour which I would rate as a Compiler bug but it could be also a language feature which I didn't know until now.

I have the following defines:

typedef union 
{
    uint32_t u32;
    uint16_t u16[2];
    uint8_t  u8[4];
    float    f;
} uMsgDataValue;

#define NFC_WATCH_MODE_IMPEDANCE            0xF7
#define NFC_WATCH_MODE_SENSITIVITY          8
#define NFC_WATCH_MODE_POLLINGINTERVAL      300

When I create now an instance of the union and initialize it, the first two initialization values are overwritten with 0. But only, when I use u8 and u16 in the initialization list. When I use only u16 or initiaize the value later via normal assignment it works.

   uMsgDataValue msgDataValue = {.u8[0] = NFC_WATCH_MODE_IMPEDANCE,
                                  .u8[1] = NFC_WATCH_MODE_SENSITIVITY,
                                 .u16[1] = NFC_WATCH_MODE_POLLINGINTERVAL};
    print("msgDataValue.u32=0x%X\n",msgDataValue.u32);
    //This prints 0x012C0000. So u8[0] and u8[1] are zero
    
    uMsgDataValue msgDataValue2 = {.u16[0] = ((uint16_t)(NFC_WATCH_MODE_SENSITIVITY)<<8) + (NFC_WATCH_MODE_IMPEDANCE),
                                 .u16[1] = NFC_WATCH_MODE_POLLINGINTERVAL};
    print("msgDataValue2.u32=0x%X\n",msgDataValue2.u32);
    //This gives the correct value of 0x012C08F7
    
    msgDataValue.u8[0] = NFC_WATCH_MODE_IMPEDANCE;
    msgDataValue.u8[1] = NFC_WATCH_MODE_SENSITIVITY;
    msgDataValue.u16[1] = NFC_WATCH_MODE_POLLINGINTERVAL;
    print("msgDataValue.u32=0x%X\n",msgDataValue.u32);
    //This prints also the correct value

Is it not allowed in c to use different integer types in the list?

I'm using SDK Version 1.9.1

Regards
Erwin

Parents Reply Children
No Data
Related