0

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 ?

0

1 Answer 1

1

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
        }
    },
};
Sign up to request clarification or add additional context in comments.

2 Comments

@Sliepen, I don't think you need extra braces around 1D arrays. Also, I get your point about unions, but why initialize only uApplicationData? You should initialize the rest TS_ApplicationFooter struct members as well (sSHA_Padding, uApplicationNonVoltatileData and TS_SHA_Digest sSHA_Digest)
@Alex: you're right, and it seems like it is a bug in GCC! I updated the answer. About not initializing the rest: as soon as you initialize one member of a struct, all members will be initialized. Those not explicitly initialized will be initialized as if they were declared static. See en.cppreference.com/w/c/language/struct_initialization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.