0
struct states
{
    float v, x;
};

struct 
{
    struct coeffs c1;
    struct states s1;
} cands;


main()
{
   // A: 
   cands.s1 = (struct states){   };
   // B:
   cands.s1 = (struct states){ 0 };
}

Microsoft compiler complains about A (syntax error), but works fine with B. GCC didn't complain about A.

Which one is right, A or B?
Are A and B the same?

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • *"Microsoft compiler complains about A,"* - complains **how** ? That specific warning/error belongs *in your question*. – WhozCraig Dec 31 '20 at 10:48
  • Does https://stackoverflow.com/questions/17589533/is-an-empty-initializer-list-valid-c-code answer your quesiton? I think there is somewhere in gcc documentation that it treats empty `{}` as `{0}`. – KamilCuk Dec 31 '20 at 10:48
  • @WhozCraig MSVC gives `syntax error`, https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2059. – Danijel Dec 31 '20 at 10:55
  • 1
    I understand. I'm saying when asking about the nature and/or validity of prospective error messages received from posted code, those error messages belong, *verbatim*, in your question; not omitted or in the general comment chain. – WhozCraig Dec 31 '20 at 10:56

1 Answers1

1

From this structure and union initializer reference:

When initializing an object of struct or union type, the initializer must be a non-empty, brace-enclosed, comma-separated list of initializers for the members

[Emphasis mine]

The correct alternative is B.

Many compilers usually add many non-standard extensions to the base language by default. Such extensions are non-portable and I recommend against using them.

GCC apparently have such an extension for the empty brace list. If you add e.g. the option -std=c11 when building then GCC will be stricter and not use non-standard extensions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621