2

Example code:

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

The above code gives warnings in gcc5.4 Below is the warning.

warning: missing braces around initializer

My understanding is that, if I want to initialize any object to 0, I can just equate to {0}.

How can I initialize an array of structs to 0 without compiler warning? Thanks.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Suresh
  • 27
  • 1
  • 8
  • You can't assign `0` to a structure, you need to assign to each structure member. – Barmar Dec 09 '16 at 00:10
  • Try `= {{0, 0, 0}}` – Barmar Dec 09 '16 at 00:10
  • 2
    try `{{0}}`. Also you have typo in `user_struct`. – BLUEPIXY Dec 09 '16 at 00:10
  • 1
    `user_stuct arr[5] = { { 0 } };` — The outer braces initialize the array as a whole, the inner braces initialize element 0 of the array, to a crude approximation. In your structure definition, you need semicolons instead of commas (or nothing) at the ends of the member definitions. You should copy'n'paste to avoid typos like that in questions on SO. – Jonathan Leffler Dec 09 '16 at 00:11
  • I should have cut+paste but typed the code on SO. Sorry for that. – Suresh Dec 09 '16 at 00:20
  • See GCC bug [53119](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119). – Jonathan Leffler Dec 09 '16 at 01:46

3 Answers3

1

The only problem with your code is that you need semicolon ; instead of , after each struct element.

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

Your initialization is ok:

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

The warning, if any, is just that you initialized but never used arr in your code.

artm
  • 17,291
  • 6
  • 38
  • 54
  • 1
    http://stackoverflow.com/questions/5434865/quickest-way-to-initialize-an-array-of-structures-to-all-0s – koper89 Dec 09 '16 at 00:17
  • thanks for the link. To quote AntT answer `You can use a simple { 0 } and it will work.` And in fact when I use single bracket, gcc did not give any wanring `-g -std=c11 -pedantic-errors -Wall -Wextra` – artm Dec 09 '16 at 00:22
  • gcc version 5.4.0 – artm Dec 09 '16 at 00:23
  • 1
    For one, this is a code snippet and want to understand why it gives "warning: missing braces around initializer" when doing user_struct arr[5] = {0}; – Suresh Dec 09 '16 at 00:25
  • @Suresh it did not give warning when I use gcc with the above flag. You can consult the answer that koper89 mentioned, also see the comment from Jonathan Leffler above – artm Dec 09 '16 at 00:28
  • @artm try `-Wmissing-braces` – BLUEPIXY Dec 09 '16 at 00:44
  • @BLUEPIXY I tried that but still no compiler warning. The flag: `-std=c11 -pedantic-errors -Wall -Wextra -Wmissing-braces` – artm Dec 09 '16 at 00:47
  • 1
    The `{ 0 }` notation has historical (and practical) precedent on its side. Some versions of GCC have complained about it; others have not. It's curious that both artm and Suresh have GCC 5.4 yet they're getting different results. FWIW: GCC 6.2.0 with options `gcc -O3 -g -std=c11 -Wall -Wextra -Werror …` on cleaned-up source (fixing the typos in the structure definition) only gives an unused variable warning, not the missing braces warning (even with `-Wmissing-braces` added). I get the same result from GCC 5.2.0, 5.3.0, 6.1.0 too. Change to `user_struct arr[5] = {0, 0, 0};` gets the warning. – Jonathan Leffler Dec 09 '16 at 00:59
  • @JonathanLeffler "only gives an unused variable warning" yes that's exactly what I've seen with gcc 5.4 as well – artm Dec 09 '16 at 01:28
  • 3
    It's puzzling that Suresh sees the warning with 5.4 and you don't, and I don't see it with surrounding versions. I'm tempted to wonder if Suresh is really using 4.5 instead of 5.4, or something similar, simply because of what you and I find (not that I'm sure that the problem occurs in GCC 4.5; I don't have a copy compiled and on hand ready to go — I'm wondering if the version number is reported accurately). _[…Brain-wave — hah; machine with GCC 4.5.1 and 4.7.1 installed!…]_ With GCC 4.5.1 and 4.7.1, I get the warning from an implicit `-Wmissing-braces` (using `-std=c99`; no C11 yet). – Jonathan Leffler Dec 09 '16 at 01:40
  • @JonathanLeffler to be exact `gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)` :) – artm Dec 09 '16 at 01:52
  • @JonathanLeffler, Using gcc5.4 but whole bunch of compilation flags are turned on and cross compiling for an arm. Only here, I get this. The gcc version is 5.4. – Suresh Dec 09 '16 at 02:09
  • @JonathanLeffler, But with gcc version 5.4 and compiling it to run on x86 doesn't give this warning. I am trying to see the difference between what I am doing in cross compilation. – Suresh Dec 09 '16 at 02:10
  • @Suresh: That's an interesting setup. Is there any chance that the 5.4 compiler driver is using a 4.x ARM cross-compiler? I'm not sure whether that's even feasible — I suspect not — but it might account for what you're seeing if it is feasible. Otherwise, I have no clue. – Jonathan Leffler Dec 09 '16 at 04:39
1

For starters there is a typo. You forgot to place a semicolon after the last data member of the structure and either you will use commas to separate declarators (that is you have to remove type specifiers before b and c) or will use semicolons to separate declarations of the data members.

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

For example you could write

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

As for the warning then this declaration

user_struct arr[5] = {0};    

declares an aggregate of aggregates that is an array of structures. It is supposed that aggregates are initialized with brace-enclosed lists. This makes initializations of aggregates more clear. So the compiler expects that you will write

user_struct arr[5] = { { 0 } };

Nevertheless your declaration is correct.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The easiest way to do this is to use memset. You can guarantee this will clear out the array and all structure members. See example below:

memset(arr, 0, sizeof(arr));

Brandon83
  • 206
  • 1
  • 7