gcc警告:标量初始化器周围的括号

时间:2010-08-11 20:27:18

标签: c lookup-tables gcc-warning

我有如下定义的查找表,我正在使用GCC。当我编译时,我得到警告

warning: braces around scalar initializer

这个警告意味着什么?我该如何初始化这个LUT?我在初始化这个结构时犯了错误吗?

帮助!!


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}}};

5 个答案:

答案 0 :(得分:21)

您应该删除大括号:{}围绕单个值。

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}}};

不会发出警告。

答案 1 :(得分:17)

在C语言中,在初始化标量值时使用额外的大括号是完全合法的,如

int x = { 5 };

即使您通常不会在现实代码中看到这一点。在你的情况下,你做的是同样的事情,除了你的情况下标量值是一个更大的聚合的成员。

GCC会为此类代码生成警告。它认为你可能写了一些你不打算写的东西,因为大多数时候大括号都是用来为聚合启动多部分初始化器,而不是标量的独立初始化器。

<rant>海湾合作委员会肯定会对集合初始化程序中有关括号的警告搞砸了。在C语言中,{ 0 }一直被用作惯用的通用零初始化器。至少{ 0 }应该免除与其习惯值相关的大括号警告。</rant>

答案 2 :(得分:5)

这是一个标量初始值设定项:int foo = 3;
这是一个带有括号的标量初始化器:int foo = {3};
这是数组的初始值设定项,它不是标量:int foo[] = {1, 2, 3};

警告说你的struct有标量初始值设定项,并带有括号:

typedef struct TECH
{

    float velocity1, velocity2;
...

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

你的代码会起作用,它的标量初始化器周围只有多余的括号。如果你把括号拿出来并将其格式化得更好(我将第一个初始化器放在它自己的行上),那就不会有任何令人反感的了。

答案 3 :(得分:3)

这意味着您无需在以下位置放置大括号:

    {74.216972},

基本上,你在那里拥有的所有大括号都是可选的(除了外部大括号),但是,你会得到一个不同的警告,因为它不包含初始化器中的嵌套结构。基本上,如果你介意警告,只在嵌套的结构和数组周围加上大括号;如果没有,请根据需要使用它们来提高可读性。

答案 4 :(得分:2)

通过用大括号包围它们来初始化标量值(velocity1,temp等),这是不需要的。

试试这个:

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}}};