Reputation: 145
I wants to initialise an array of struct variable, while the struct itself consists of array of bytes
struct my_bytes {
u8 byte[128];
};
struct my_bytes data[] = {
{ 0x12, 0x34, 0x56, 0x78 },
{ 0x13, 0x35, 0x57, 0x79 },
{ 0x14, 0x36, 0x58, 0x7a },
};
Compile is fine in native gcc 4.8.5 but error in other compiler/environment Is there another way to initialise data?
Error message
it_sram.c:200:3: error: missing braces around initializer [-Werror=missing-braces]
it_sram.c:200:3: error: (near initialization for 'data[0].byte') [-Werror=missing-braces]
it_sram.c:199:18: error: unused variable 'data' [-Werror=unused-variable]
it_sram.c: At top level:
cc1: error: unrecognized command line option "-Wno-misleading-indentation" [-Werror]
cc1: all warnings being treated as errors
Upvotes: 0
Views: 91
Reputation: 409176
You need two pairs of curcly braces {}
:
struct my_bytes data[] = {
{ { 0x12, 0x34, 0x56, 0x78 } },
{ { 0x13, 0x35, 0x57, 0x79 } },
{ { 0x14, 0x36, 0x58, 0x7a } },
};
The outer is for the structure, the inner for the array.
Upvotes: 2
Reputation: 32586
you missed a level of {}
struct my_bytes data[] = {
{{ 0x12, 0x34, 0x56, 0x78 } },
{{ 0x13, 0x35, 0x57, 0x79 } },
{{ 0x14, 0x36, 0x58, 0x7a } },
};
to have that more visible if I change the struct to be :
struct my_bytes {
u8 byte[128];
int a;
};
you need something like that :
struct my_bytes data[] = {
{{ 0x12, 0x34, 0x56, 0x78 }, 1 },
{{ 0x13, 0x35, 0x57, 0x79 }, 2 },
{{ 0x14, 0x36, 0x58, 0x7a }, 3 },
};
Upvotes: 2