30

I have look-up-table as defined below and I'm making use of GCC. When I compile I get warnings as

warning: braces around scalar initializer

What does this warning mean? How should I initialize this LUT? Am I making a mistake in initializing this structures?

Help!!


typedef struct TECH
{

    float velocity1, velocity2;
    float temp;
    float measure;

    int id;
    float storage[64];

}TECH;

struct TECH lut_model_1[2] = {{{296.001465},
        {74.216972},
        {2.025908},
        {1.516384},
        {1},
        {0.001746,
        0.000256, 0.006216, 0.005249, -0.001668, -0.001377, 0.009865, 0.010454, -0.000288, -0.005853, 0.010584, 0.015440, 0.000465, -0.000602, 0.004330, 0.005700, 0.017120,
        0.233015, 0.034154, 0.244022, 0.007644, 0.385683, 0.042960, 0.406633, -0.007811, 0.346931, 0.040123, 0.387361, 0.007030, 0.225309, 0.017897, 0.241024, 0.003700,
        0.103601, 0.060748, 0.121059, -0.045041, 0.076974, 0.070647, 0.148810, -0.022399, 0.074007, 0.054797, 0.141794, 0.010376, 0.052482, 0.045013, 0.078443, -0.019940,
        -0.057353, 0.044285, 0.066622, -0.058232, -0.093817, 0.064753, 0.126611, -0.008286, -0.085634, 0.029582, 0.140443, 0.009189, -0.052974, 0.036057, 0.087536}},

        {{309.270569},
        {74.520226},
        {2.088673},
        {1.595730},
        {1},
        {-0.003261,
        0.001452, 0.006673, 0.007092, 0.001020, 0.002904, 0.009037, 0.009587, -0.001494, 0.000296, 0.009327, 0.010013, -0.000301, -0.002727, 0.005875, 0.008888, -0.016850,
        0.231185, 0.029758, 0.241629, 0.009411, 0.382748, 0.057553, 0.407984, -0.019496, 0.393691, 0.045355, 0.411033, -0.019787, 0.185746, 0.027101, 0.216863, 0.010189,
        0.050463, 0.041380, 0.059462, 0.009747, 0.093188, 0.089831, 0.132579, -0.049612, 0.058789, 0.075130, 0.122026, -0.022185, 0.017041, 0.035450, 0.074255, -0.002068,
        -0.061219, 0.040752, 0.087084, -0.013021, -0.106098, 0.066566, 0.140099, -0.041966, -0.073433, 0.055231, 0.125908, -0.003481, -0.050690, 0.017257, 0.085251}}};
HaggarTheHorrible
  • 7,083
  • 20
  • 70
  • 81
  • I have the same issue, only I'm stuck with an ERROR. "error: braces around scalar initializer for type 'float'". I would be happy if I knew how to make this a compiler warning, as code I need to compile is not mine to edit. – user297500 Mar 14 '14 at 20:59
  • @user297500, initialize with `0.f` – Ivan Kush Dec 03 '16 at 14:48

5 Answers5

29

You should remove the braces: { and } around single values.

struct TECH lut_model_1[2] = {{296.001465,
        74.216972,
        2.025908,
        1.516384,
        1,
        {0.001746,
        0.000256, 0.006216, 0.005249, -0.001668, -0.001377, 0.009865, 0.010454, -0.000288, -0.005853, 0.010584, 0.015440, 0.000465, -0.000602, 0.004330, 0.005700, 0.017120,
        0.233015, 0.034154, 0.244022, 0.007644, 0.385683, 0.042960, 0.406633, -0.007811, 0.346931, 0.040123, 0.387361, 0.007030, 0.225309, 0.017897, 0.241024, 0.003700,
        0.103601, 0.060748, 0.121059, -0.045041, 0.076974, 0.070647, 0.148810, -0.022399, 0.074007, 0.054797, 0.141794, 0.010376, 0.052482, 0.045013, 0.078443, -0.019940,
        -0.057353, 0.044285, 0.066622, -0.058232, -0.093817, 0.064753, 0.126611, -0.008286, -0.085634, 0.029582, 0.140443, 0.009189, -0.052974, 0.036057, 0.087536}},

        {309.270569,
        74.520226,
        2.088673,
        1.595730,
        1,
        {-0.003261,
        0.001452, 0.006673, 0.007092, 0.001020, 0.002904, 0.009037, 0.009587, -0.001494, 0.000296, 0.009327, 0.010013, -0.000301, -0.002727, 0.005875, 0.008888, -0.016850,
        0.231185, 0.029758, 0.241629, 0.009411, 0.382748, 0.057553, 0.407984, -0.019496, 0.393691, 0.045355, 0.411033, -0.019787, 0.185746, 0.027101, 0.216863, 0.010189,
        0.050463, 0.041380, 0.059462, 0.009747, 0.093188, 0.089831, 0.132579, -0.049612, 0.058789, 0.075130, 0.122026, -0.022185, 0.017041, 0.035450, 0.074255, -0.002068,
        -0.061219, 0.040752, 0.087084, -0.013021, -0.106098, 0.066566, 0.140099, -0.041966, -0.073433, 0.055231, 0.125908, -0.003481, -0.050690, 0.017257, 0.085251}}};

will give no warning.

mvds
  • 45,755
  • 8
  • 102
  • 111
22

In C language it is perfectly legal to use extra braces when initializing a scalar value, as in

int x = { 5 };

even though you won't normally see this in real-life code. In your case you are doing the same thing, except that in your case the scalar value is a member of a larger aggregate.

GCC generates warnings for code like that. It believes that it is possible that you wrote something you didn't intend to write, since braces are most of the time are used to start a multi-part initializer for an aggregate, not a standalone initializer for a scalar.

<rant>GCC is definitely screwing things up with its warnings about braces in aggregate initializers. In C language the { 0 } has always been used as an idiomatic universal zero-initializer. At least { 0 } should have been made exempt from brace-related warnings for its idiomatic value.</rant>

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    Does this mean that `{ 0 }` also should be a perfectly legal initializer for an aggregate whose first member is another aggregate? – jamesdlin Aug 12 '10 at 03:01
  • 2
    @jamesdlin: Yes. There's no need to open a dedicated level of `{}` for each nested aggregate. GCC will issue a warning though - another place where they managed to break the `{ 0 }` idiom with annoying warnings. – AnT stands with Russia Aug 12 '10 at 04:29
  • This annoys me too. I keep getting warnings for loads of situations where I want a structure to get initialised to zero with `= {0};` and it moans. They should make a special case for `= {0};` and ease the warnings. – Matt Clarkson Aug 09 '11 at 18:40
  • Is `= {}'` not idiomatic enough? See Example 4 on page 142 (160): http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf – Vorac Apr 22 '14 at 08:12
  • @Vorac: I don't see anything relevant in Example 4 on page 142 in the linked document. Moreover, I don't see anything there that would support the `= {}` syntax in C. – AnT stands with Russia Apr 22 '14 at 17:58
  • @AndreyT, hmm, strange. The page is 142 written on the sheet, but for the pdf the page is 160. The example is that if one provides initializer, but doesn't provide values to all fields, the rest are initialized to 0. (btw you have +1 from me) – Vorac Apr 23 '14 at 07:39
  • 2
    @Vorac: Yes, I understand the mismatch in numbering and I'm sure I was was looking at the right example. The feature that initializes "the rest" of the aggregate with zero has been in the language since the beginning of times. However, that does not mean that the `= {}` syntax is legal. C language grammar explicitly requires at least one initializer between the `{}`, i.e. the `= { 0 }` is the shortest way to initialize everything to zero, while `= {}` is a syntax error. It is C++ that supports `= {}`, but not C. – AnT stands with Russia Apr 23 '14 at 15:28
  • @AndreyT, my apology. I am using gcc without `-std=`, so what I am observing is probably some gcc extension. – Vorac Apr 24 '14 at 08:09
5

This is a scalar initializer: int foo = 3;
This is a scalar initializer with braces around it: int foo = {3};
This is an initializer of an array, which isn't scalar: int foo[] = {1, 2, 3};

The warning says that your struct has scalar initializers with braces around them:

typedef struct TECH
{

    float velocity1, velocity2;
...

struct TECH lut_model_1[2] = {{{296.001465},
    {74.216972},
...

Your code will work, it just has superfluous braces around its scalar initializers. If you take the braces out and format it a little more nicely (I'd put the first initializer on its own line) there will be nothing objectionable about it.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
3

This means you needn't put braces in places like:

    {74.216972},

Basically, all the braces you have there are optional (except the outer ones), however, you will get a different warning for not embracing nested structs in the initializer. Basically, if you mind the warning, put braces around nested structs and arrays only; if not, use them to improve readability as you like.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
2

You initialize scalar values (velocity1, temp, etc) by surrounding them with braces, which is not needed.

Try this:

struct TECH lut_model_1[2] = {{296.001465,
        74.216972,
        2.025908,
        1.516384,
        1,
        {0.001746,
        0.000256, 0.006216, 0.005249, -0.001668, -0.001377, 0.009865, 0.010454, -0.000288, -0.005853, 0.010584, 0.015440, 0.000465, -0.000602, 0.004330, 0.005700, 0.017120,
        0.233015, 0.034154, 0.244022, 0.007644, 0.385683, 0.042960, 0.406633, -0.007811, 0.346931, 0.040123, 0.387361, 0.007030, 0.225309, 0.017897, 0.241024, 0.003700,
        0.103601, 0.060748, 0.121059, -0.045041, 0.076974, 0.070647, 0.148810, -0.022399, 0.074007, 0.054797, 0.141794, 0.010376, 0.052482, 0.045013, 0.078443, -0.019940,
        -0.057353, 0.044285, 0.066622, -0.058232, -0.093817, 0.064753, 0.126611, -0.008286, -0.085634, 0.029582, 0.140443, 0.009189, -0.052974, 0.036057, 0.087536}},

        {309.270569,
        74.520226,
        2.088673,
        1.595730,
        1,
        {-0.003261,
        0.001452, 0.006673, 0.007092, 0.001020, 0.002904, 0.009037, 0.009587, -0.001494, 0.000296, 0.009327, 0.010013, -0.000301, -0.002727, 0.005875, 0.008888, -0.016850,
        0.231185, 0.029758, 0.241629, 0.009411, 0.382748, 0.057553, 0.407984, -0.019496, 0.393691, 0.045355, 0.411033, -0.019787, 0.185746, 0.027101, 0.216863, 0.010189,
        0.050463, 0.041380, 0.059462, 0.009747, 0.093188, 0.089831, 0.132579, -0.049612, 0.058789, 0.075130, 0.122026, -0.022185, 0.017041, 0.035450, 0.074255, -0.002068,
        -0.061219, 0.040752, 0.087084, -0.013021, -0.106098, 0.066566, 0.140099, -0.041966, -0.073433, 0.055231, 0.125908, -0.003481, -0.050690, 0.017257, 0.085251}}};
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83