gcc 警告:标量初始值设定项周围有大括号

发布于 2024-09-14 07:05:00 字数 1953 浏览 7 评论 0原文

我有如下定义的查找表,并且正在使用 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}}};

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

流年里的时光 2024-09-21 07:05:00

您应该删除单个值周围的大括号:{}

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

不会发出任何警告。

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.

瑕疵 2024-09-21 07:05:00

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

int x = { 5 };

您通常不会在现实代码中看到这种情况。在您的情况下,您正在做同样的事情,只不过在您的情况下标量值是更大聚合的成员。

GCC 会针对此类代码生成警告。它认为您可能编写了一些您不打算编写的内容,因为大括号大多数时候用于启动聚合的多部分初始化程序,而不是标量的独立初始化程序。

GCC 肯定会因为它对聚合初始值设定项中大括号的警告而把事情搞砸了。在 C 语言中,{ 0 } 始终被用作惯用的通用零初始化器。至少 { 0 } 应该因其惯用值而免受大括号相关警告的影响。

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>

金橙橙 2024-09-21 07:05:00

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

该警告表明您的结构具有带有大括号的标量初始值设定项:

typedef struct TECH
{

    float velocity1, velocity2;
...

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

您的代码会起作用,它只是在其标量初始值设定项周围有多余的大括号。如果你去掉大括号并将其格式化得更好一点(我将第一个初始化器放在自己的行上),那么就没有什么令人反感的了。

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.

花落人断肠 2024-09-21 07:05:00

这意味着您不需要将大括号放在如下位置:

    {74.216972},

基本上,您拥有的所有大括号都是可选的(外部大括号除外),但是,如果在初始化程序中不包含嵌套结构,您将收到不同的警告。基本上,如果您介意警告,请仅在嵌套结构和数组周围放置大括号;如果没有,请根据需要使用它们来提高可读性。

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.

演出会有结束 2024-09-21 07:05:00

您可以通过用大括号括住标量值(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}}};

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}}};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文