This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

const struct/unon initialization proble.

Hi,

with the following sample code...

typedef __packed struct
{
        uint8_t field_1;
        uint8_t field_2;
        uint8_t field_3;
        uint8_t field_4;
} T_Sub;

typedef __packed struct
{
        T_Sub   Sub;
        uint8_t field_1;
        uint8_t field_2;
        uint8_t field_3;
        uint8_t field_4;
} T_Data;

typedef __packed union
{
        T_Data  content;
        uint8_t buffer[sizeof(T_Data)];
} T_Union;

const T_Union C_Union = { {1,2,3,4}, 1, 2, 3, 4, 5};

// The following (no union, just strcut) has the same problem
/*
typedef __packed struct
{
        T_Data  content;
} T_Struct;

const T_Struct C_Struct = { {1,2,3,4}, 1, 2, 3, 4, 5};
*/


I get:

error:  #146: too many initializer values

I'm using uVision4.
The same code works on other compilers. I'm obviously missing something big but I can't figure what...

Parents
  • @Stefan HartwigPosted
    Yes, it works... kinda fussy :-)

    @stephen phillipsPosted
    According to many C bibles, the first field of the union is the one that is assigned., there shoud no be ambiguity...
    My code does use macros but I wroted a simple example to exlcude eventual macro errors...

    Thank you

Reply
  • @Stefan HartwigPosted
    Yes, it works... kinda fussy :-)

    @stephen phillipsPosted
    According to many C bibles, the first field of the union is the one that is assigned., there shoud no be ambiguity...
    My code does use macros but I wroted a simple example to exlcude eventual macro errors...

    Thank you

Children
  • Yes, it works... kinda fussy :-)
    It isn't fussy at all. Strip down what you are doing:

    typedef __packed struct
    {
            uint8_t field_1;
            uint8_t field_2;
            uint8_t field_3;
            uint8_t field_4;
    } T_Sub;
    
    const T_Sub T_Sub_example = {1, 2, 3, 4};
    
    typedef __packed struct
    {
            T_Sub Sub;
            uint8_t field_1;
            uint8_t field_2;
            uint8_t field_3;
            uint8_t field_4;
    } T_Data;
    
    const T_Data T_Data_example =
            {
                    /* T_Sub_example */ {1, 2, 3, 4},
                    /* field_1 to field_4 */ 5, 6, 7, 8
            };
    
    typedef __packed union
    {
            T_Data  content;
            uint8_t buffer[sizeof(T_Data)];
    } T_Union;
    
    const T_Union T_Union_example =
            {
            /* T_Data_example */
                    {
                    /* T_Sub_example */ {1, 2, 3, 4},
                    /* field_1 to field_4 */ 5, 6, 7, 8
                    }
            /* empty since the array "buffer" is not initialized */
            };