c - C 中结构体数组初始化为 0,警告 : missing braces around initializer

标签 c initialization warnings

示例代码:

#include <stdio.h>

typedef struct
{
  unsigned int a,
  unsigned int b,
  unsigned int c
} user_struct;


int main()
{
  user_struct arr[5] = {0};    // gives warning on compilation
  return 0;
}

上面的代码在gcc5.4中给出了警告 下面是警告。

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

我的理解是,如果我想将任何对象初始化为 0,我可以等同于 {0}。

如何将结构数组初始化为 0 而不会出现编译器警告?谢谢。

最佳答案

代码的唯一问题是每个结构元素后面需要分号 ; 而不是 ,

    typedef struct
    {
        unsigned int a;
        unsigned int b;
        unsigned int c;
    } user_struct;

你的初始化没问题:

user_struct arr[5] = {0};  // this should memset all array elements (here struct) to zero

警告(如果有)只是您已初始化但从未在代码中使用过 arr

关于c - C 中结构体数组初始化为 0,警告 : missing braces around initializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051053/

相关文章:

c++ - 在类定义中初始化同一类的对象时出错 - C++

c++ - 我怎样才能打开(字面上)所有 GCC 的警告?

c++ - 带有 g++ 的不需要的警告消息

objective-c - 警告 : Capturing $XXX strongly in this block is likely to lead to a retain cycle in 'AFHTTPClient.m'

c++ - 如何在编译过程中编辑可执行文件而不更改其源代码?

java - 在JavaFX Controller 类中添加向initialize()抛出异常的方法

c - 简单 C 代码中的指针数组问题

C# 如果没有 'new' ,这个初始化会做什么?

c - 除非初始化,否则分配的内存不使用?

c - 我怎样才能编译和运行这个项目?