Antoine Boré
Antoine Boré

Reputation: 93

Initialize union inside of struct: cannot find why Missing brace around initializer

I am trying to initialize properly this struct:

typedef struct
{
    TU_ApplicationData uApplicationData;
    TS_SHA_Padding sSHA_Padding;
    TU_ApplicationNonVolatileData uApplicationNonVoltatileData;
    TS_SHA_Digest sSHA_Digest;
}TS_ApplicationFooter;

As you can see, it is composed with other structure:

typedef struct
{
    uint32_t u32ApplicationVersion_Major;
    uint32_t u32ApplicationVersion_Minor;
    fpJumpHandler fpApplicationJumpHandler;
    uint32_t u32BootApplicationStartAddress;
    uint32_t u32BootApplicationAllocationSize;
    uint32_t u32UserApplicationStartAddress;
    uint32_t u32UserApplicationAllocationSize;
}TS_ApplicationData;

typedef union
{
    uint32_t au32ApplicationData[32];
    TS_ApplicationData sApplicationData;
}TU_ApplicationData;

typedef struct
{
    uint32_t u32SHA_1PaddingBytes[16];
}TS_SHA_Padding;

typedef struct
{
    uint8_t u8ApplicationNonVoltatileData[256];
}TU_ApplicationNonVolatileData;

typedef struct
{
    uint32_t au32ApplicationHashTag[16];
}TS_SHA_Digest;

I cannot find the proper way to initialize the TS_ApplicationFooter structure. The best way that I have is the following, but it returns me the warning: "missing braces around initializer [-Wmissing-braces]".

const TS_ApplicationFooter sUserApplicationFooter =
{ /* Warning point here */
    .uApplicationData=
    {
        .au32ApplicationData={0},
        .sApplicationData=
        {
            .u32ApplicationVersion_Major=U32_USB_CDC_USER_APP_SW_RELEASE_MAJOR_VERSION,
            .u32ApplicationVersion_Minor=U32_USB_CDC_USER_APP_SW_RELEASE_MINOR_VERSION,
            .fpApplicationJumpHandler=NULL,
            .u32BootApplicationStartAddress=U32_BOOT_LOADER_APPLICATION_START_ADDRESS,
            .u32BootApplicationAllocationSize=U32_BOOT_LOADER_APPLICATION_ALLOCATED_SIZE,
            .u32UserApplicationStartAddress=U32_USER_APPLICATION_START_ADDRESS,
            .u32UserApplicationAllocationSize=U32_USER_APPLICATION_ALLOCATED_SIZE
        }
    },
    .sSHA_Padding={0},
    .uApplicationNonVoltatileData={0},
    .sSHA_Digest={0}
};

Do you have any idea on how I could initialize this structure without having any warning of this kind ?

Upvotes: 0

Views: 453

Answers (1)

G. Sliepen
G. Sliepen

Reputation: 7973

You should add extra braces around the last three members to appease GCC:

    ...
    .sSHA_Padding={{0}},
    .uApplicationNonVoltatileData={{0}},
    .sSHA_Digest={{0}}
};

This might be a bug in GCC. Clang does not show this warning, even if compiled with -Wall -W -Wmissing-braces -pedantic.

Alternatively, since as soon as you initialize one member member of a struct, all other members that are not explicitly initialized are initialized to zero, you can just omit them.

Furthermore, you should only initialize exactly one member of a union! I recommend you rewrite the whole initialization like so:

const TS_ApplicationFooter sUserApplicationFooter =
{
    .uApplicationData =
    {
        .sApplicationData =
        {
            .u32ApplicationVersion_Major = U32_USB_CDC_USER_APP_SW_RELEASE_MAJOR_VERSION,
            .u32ApplicationVersion_Minor = U32_USB_CDC_USER_APP_SW_RELEASE_MINOR_VERSION,
            .u32BootApplicationStartAddress = U32_BOOT_LOADER_APPLICATION_START_ADDRESS,
            .u32BootApplicationAllocationSize = U32_BOOT_LOADER_APPLICATION_ALLOCATED_SIZE,
            .u32UserApplicationStartAddress = U32_USER_APPLICATION_START_ADDRESS,
            .u32UserApplicationAllocationSize = U32_USER_APPLICATION_ALLOCATED_SIZE
        }
    },
};

Upvotes: 1

Related Questions