How to repair warning: missing braces around initializer?
Asked Answered
C

2

65

The warning is produced by the c code generated by vala.

warning: missing braces around initializer

The code works but the warning is annoying. The vala code referenced by the warning is

struct Position {uint x; uint y;}
private static Position positions[8];

The generated C code is

static Position det_positions[8] = {0};

I've tried initializing positions half a dozen different ways but can't seem to get the syntax to satisfy the warning. Is this GCC bug 53119 or is there a way to fix it?

Confound answered 6/12, 2012 at 14:45 Comment(2)
I don't get this warning with Vala 0.16 or 0.18 on GCC 4.6.3. Care to share your environment? Also, what is the generated C that is causing the problem?Haematoxylon
@Haematoxylon The embedded linux target is a Leopardboard 368, the GCC is 4.4.1, the toolchain is codesourcery/arm-2010q1, and I've added the generated C to the question. I've also properly formatted the code which I forgot to do earlier.Confound
H
84

Yes, this appears to be related to GCC bug 53119. It goes away if you change the C declaration to {{0}}. Your options are:

  1. Ignore the warning.
  2. Manipulate the C code after generation to have {{0}} instead of {0} on that line using sed or the like.
  3. Declare the array extern in Vala, and write the C definition elsewhere. (The permanent version of #2.)
  4. Do something like struct foo { int bar; Position positions[8]; } static foo position_holder and {0} will then be initialising position_holder.bar which is fine and the warning goes away.
Haematoxylon answered 7/12, 2012 at 6:50 Comment(2)
Or add "-Wno-missing-braces" to your compile to silence the invalid warning.Bloodshot
Note, however, that the warning is not invalid in general! Just for the zero initialization (i.e. = {0}), it is always invalid. See Adit Ya's answer for a valid case for the warning.Rance
S
20

This warning also appears when a multi-dimensional array is treated as a linear array ( although it is still correct and the code runs perfectly ) with -Wall compiler flags set.

For example

char array[5][10][2] = {\
"0","0","0","0","0","0","0","0","0","0",\
"1","1","1","1","1","1","1","1","1","1",\
"2","2","2","2","2","2","2","2","2","2",\
"3","3","3","3","3","3","3","3","3","3",\
"4","4","4","4","4","4","4","4","4","4" };

This will generate the warning.

Do the following changes to remove the warnings as shown below

char array[5][10][2] = {\
{"0","0","0","0","0","0","0","0","0","0" },\
{"1","1","1","1","1","1","1","1","1","1"},\
{"2","2","2","2","2","2","2","2","2","2"},\
{"3","3","3","3","3","3","3","3","3","3"},\
{"4","4","4","4","4","4","4","4","4","4"} };

Please do correct me if I am wrong.

Silda answered 19/2, 2014 at 7:17 Comment(1)
In this example the first and second level of the array are entered with {, and the third by defining strings (ending in '\0') entered with ". In 2D, if you also use the strings, then use only one level of {: {"0"} i.e. char '0' then char '\0' followed by zeros and if you don't use strings then two levels of {: {{0}} i.e. zeros only.Null

© 2022 - 2024 — McMap. All rights reserved.