0
  1. const char* testarr[]={{},};
  2. const char* testarr[]={{}};

Both of these statements are compiled and works well (during a test code). But I do not understand why the first statement (with a comma - , ) succeeded. Any thoughts or comments?

To my senses - the unnecessary comma should cause it to fail.
Environment : Linux, g++, and this is in global area of a header file.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Rahul Das
  • 115
  • 1
  • 6
  • Are you using c or c++? They are different languages and might have different answers – Alan Birtles Apr 28 '23 at 18:43
  • 1
    Disregarding the fact that empty arrays are not legal in standard C, no difference. The trailing comma in an initializer list is ignored. – Eugene Sh. Apr 28 '23 at 18:43
  • 1
    [Why is a trailing comma in an initializer-list allowed?](https://stackoverflow.com/questions/7043372/int-a-1-2-why-is-a-trailing-comma-in-an-initializer-list-allowed) – Drew Dormann Apr 28 '23 at 18:45
  • @EugeneSh. I think they both have a single element (at least in c++ https://godbolt.org/z/rM1zeGEc9)? – Alan Birtles Apr 28 '23 at 18:46
  • @AlanBirtles As I said - in standard C they are not allowed. Different extensions may implement it differently. I suspect the same is true for C++, but not an expert in it. – Eugene Sh. Apr 28 '23 at 18:49
  • Sorry - forgot to mention : In my case , it is global. – Rahul Das Apr 28 '23 at 18:49
  • gcc certainly complains about the code in c mode: https://godbolt.org/z/asn9hrsWx – Alan Birtles Apr 28 '23 at 18:53
  • @AlanBirtles It complains about other things though.. Here is simplified test for both C and C++ with `pedantic` warnings: https://godbolt.org/z/azYnsz6cq , https://godbolt.org/z/sP13hzEbb - both are complaining about array of zero size. – Eugene Sh. Apr 28 '23 at 18:58
  • @EugeneSh. that's different code in c++ though, the original code is a perfectly valid way of creating an array containing a single null pointer – Alan Birtles Apr 28 '23 at 19:01

0 Answers0