将结构数组初始化为全 0 的最快方法?

发布于 2024-10-26 19:37:17 字数 169 浏览 5 评论 0原文

我正在尝试使用以下语法将结构数组初始化为全 0:

STRUCTA array[MAX] = {0};

但是,我从 gcc 收到以下警告:

警告:初始化程序周围缺少大括号

我做错了什么 - 是否有另一种/更好的方法要这样做吗?

I'm trying to initialize an array of structures to all-0's, using the below syntax:

STRUCTA array[MAX] = {0};

However, I'm getting the following warning from gcc :

warning: missing braces around initializer

What am i doing wrong - is there another/better way to do this ?

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

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

发布评论

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

评论(7

绝影如岚 2024-11-02 19:37:17

如果结构体的第一个成员具有标量类型,则使用

STRUCTA array[MAX] = {{ 0 }};

如果结构体的第一个成员恰好是另一个结构体对象,其第一个成员具有标量类型,那么您将不得不使用

STRUCTA array[MAX] = {{{ 0 }}};

,依此类推。基本上,每次“输入”另一个嵌套聚合(结构体或数组)时,您都必须打开新级别的嵌套{}。因此,在这种情况下,只要每个嵌套聚合的第一个成员也是聚合,您就需要更深入地了解{}

所有这些额外的大括号只是为了避免警告。当然,这只是一个无害的警告(在本例中)。您可以使用简单的 { 0 } 就可以了。

处理此问题的最佳方法可能是完全禁用此警告(有关正确的命令行选项,请参阅@pmg 的答案)。从事 GCC 工作的人没有思考清楚。我的意思是,我理解该警告的价值(它确实非常有用),但破坏 { 0 } 的功能是不可接受的。 { 0 } 应该受到特殊对待。

It the first member of your struct has a scalar type, use

STRUCTA array[MAX] = {{ 0 }};

If the first member of your struct happens to be another struct object, whose first member has scalar type, then you'll have to use

STRUCTA array[MAX] = {{{ 0 }}};

and so on. Basically, you have to open a new level of nested {} every time you "enter" another nested aggregate (a struct or an array). So in this case as long as the first member of each nested aggregate is also an aggregate, you need to go deeper with {}.

All these extra braces are only there to avoid the warning. Of course, this is just a harmless warning (in this specific case). You can use a simple { 0 } and it will work.

Probably the the best way to deal with this is to disable this warning entirely (see @pmg's answer for the right command-line option). Someone working on GCC wasn't thinking clearly. I mean, I understand the value of that warning (and it can indeed be very useful), but breaking the functionality of { 0 } is unacceptable. { 0 } should have been given special treatment.

死开点丶别碍眼 2024-11-02 19:37:17

gcc 很麻烦。它应该接受这一点而不发出警告。
试试这个

STRUCTA array[MAX] = {{0}};

gcc 行为可以通过选项 -Wmissing-braces-Wno-missing-braces 来控制。

-Wall 启用此警告;要具有 -Wall 但不缺少缺失的大括号,请使用 -Wall -Wno-missing-braces

gcc is being a nuisance. It should accept that without warnings.
Try this

STRUCTA array[MAX] = {{0}};

That gcc behaviour can be controlled with the option -Wmissing-braces or -Wno-missing-braces.

-Wall enables this warning; to have -Wall but not the missing braces, use -Wall -Wno-missing-braces

猫九 2024-11-02 19:37:17

这只是 gcc 发出的有害警告,我将使用 -Wno-braces 禁用它。 {0} 是一个非常有用的“通用零初始值设定项”,适用于您的代码不应该知道其定义的类型,而 gcc 不鼓励使用它对追求良好代码非常有害。

如果 gcc 希望保留此警告,则至少应该使用特殊情况 {0} 并在这种情况下禁用警告。

This is merely a harmful warning issued by gcc, and I would disable it with -Wno-braces. {0} is an extremely useful "universal zero initializer" for types whose definition your code is not supposed to be aware of, and gcc's discouraging its use is actively harmful to the pursuit of good code.

If gcc wants to keep this warning, it should at least special-case {0} and disable the warning in this one case.

凡尘雨 2024-11-02 19:37:17

您可以通过使用完全空的大括号来避免警告:

STRUCTA array[10] = {};

该数组将被聚合初始化,这意味着每个结构体它将依次为 值初始化。带空括号的值初始化变成每个结构体的 aggregate-initializion ,其中将所有字段设置为 0,这就是您想要的。

只要您的结构是 POD,这在所有情况下都有效(请参阅上面的链接以获取准确的描述)。

You can avoid the warning by using completely empty braces:

STRUCTA array[10] = {};

The array will be aggregate-initialized, which means that each of the structs in it will in turn be value-initialized. Value-initialization with empty brackets turns into aggregate-initializion of each struct, which sets all fields to 0, which is what you want.

This works in all cases as long as your structs are POD (see the links above for precise description).

酒废 2024-11-02 19:37:17

数组是用大括号初始化的,结构体也是如此。您可能需要在 0 周围放置一组额外的大括号,并且根据 STRUCTA 的定义方式,一些额外的 0 用逗号分隔。

Arrays are initialized with braces, but so are structs. You probably need to put an extra set of braces around your 0 and, depending on how STRUCTA is defined, some extra 0s separated by commas.

夜灵血窟げ 2024-11-02 19:37:17

这取决于结构。例如 :

typedef struct structa 
{
    int a, b;
} STRUCTA;

int main (int argc, char const* argv[])
{
    STRUCTA array[10] = {{0,0}};
    return 0;
}

It depends on the STRUCTA. For example :

typedef struct structa 
{
    int a, b;
} STRUCTA;

int main (int argc, char const* argv[])
{
    STRUCTA array[10] = {{0,0}};
    return 0;
}
月亮坠入山谷 2024-11-02 19:37:17

STRUCTA 可以赋值为0吗?

您也可以随时使用 memset()。

Can STRUCTA be assigned to 0?

You can always use memset(), too.

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