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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5434865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:09:58  来源:igfitidea点击:

Quickest way to initialize an array of structures to all-0's?

cinitializationstructure

提问by TCSGrad

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

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

STRUCTA array[MAX] = {0};

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

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

warning: missing braces around initializer

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

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

我做错了什么 - 还有另一种/更好的方法来做到这一点吗?

回答by AnT

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 firstmember 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.

所有这些额外的括号只是为了避免警告。当然,这只是一个无害的警告(在这种特定情况下)。你可以使用一个简单的{ 0 },它会工作。

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.

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

回答by pmg

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

gcc 很麻烦。它应该在没有警告的情况下接受这一点。
尝试这个

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

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

可以使用选项-Wmissing-braces-Wno-missing-braces.

-Wallenables this warning; to have -Wallbut not the missing braces, use -Wall -Wno-missing-braces

-Wall启用此警告;有-Wall但不是缺少大括号,使用-Wall -Wno-missing-braces

回答by R.. GitHub STOP HELPING ICE

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.

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

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

如果 gcc 想要保留此警告,它至少应该{0}在这种情况下进行特殊情况并禁用警告。

回答by Pablo Alvarez

You can avoid the warning by using completely empty braces:

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

STRUCTA array[10] = {};

结构数组[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-initializionof each struct, which sets all fields to 0, which is what you want.

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

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

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

回答by Rumple Stiltskin

It depends on the STRUCTA. For example :

这取决于 STRUCTA。例如 :

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

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

回答by nmichaels

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 STRUCTAis defined, some extra 0s separated by commas.

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

回答by tamarintech

Can STRUCTA be assigned to 0?

STRUCTA 可以赋值为 0 吗?

You can always use memset(), too.

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