Please note: We are aware of an issue affecting replies on the Arm Community forums, which may not be loading as expected.
We apologize for any inconvenience and appreciate your patience while we investigate and work to resolve the issue.
Thank you for your understanding.
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...
sorry... the correct code is
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};
and I get the same error (the 5th initializer was just a test to check if I get the same error)
try:
const T_Union C_Union = {{ {1,2,3,4}, 1, 2, 3, 4}};
Ummm you may need to consider WHAT structure you are initializing a union can be either structure, so it's possible it could be assuming it's the array portion of the union to which there are too many for a single element ( 4 by viewing your code ). I would suggest you use type T_Data for your constant and not the union. Otherwise you have an ambiguity in which type of structure you are initializing.
Further more I strong urge you to use MACRO's to form any such structure constants because of one typo can create unexpected results very easily. Save all the drudgery for the PREPROCESSOR I always say.
Stephen
@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
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 */ };