typedef struct_t struct_array[ROWS][COLS];
int main()
{
struct_array structArray1 = {0};
}
I got an error saying there is a missing braces around the initializer. I know there is a bug of gcc regarding this warning. Or am I doing something wrong?
You need to use struct_array structArray1 = {{{0}}};, the first one for the 1st dimension of the array, the 2nd one for the 2nd dimension and the third for the struct initialization. The code is right, but your GCC is buggy as stated in other answers.
Your code is completely correct. And you're right that GCC has a bug, too - it's described here.
You have a couple of choices:
Disable -Wmissing-braces for now.
Use empty initalizer braces (GCC extension):
struct_array structArray1 = {};
Initialize one complete object. For a three-element struct_t, for example:
struct_array structArray1 = { { { 0, 0, 0 } } };
Specify all of the necessary braces and zeroes. Assuming the same structure type as in #3 above, and a 2x2 array:
struct_array structArray1 = { { { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 } },
{ { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 } } };
Use a different compiler. clang, maybe?
Fix the bug in GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With