1
int x[1000] = {0, }; 

This is about the initialization, where a , appears without an operand on its right.

My compiler reports an error.

Thinking it might(?) differ from both C and C++, I am tagging both of them.

Masse
  • 11
  • 1
  • It looks OK to me. It might help if you add which compiler (and version) you are using and the error message it gives. – Daniel Frey Sep 30 '13 at 10:36
  • 2
    http://stackoverflow.com/questions/7043372/int-a-1-2-weird-comma-allowed-any-particular-reason and http://stackoverflow.com/questions/2311864/history-of-trailing-comma-in-programming-language-grammars – willj Sep 30 '13 at 10:37
  • @DanielFrey: Some of the leading gcc ones - Yes, I tried in three of them, plus the Dev one. Tried the online IDEs as well. – Masse Sep 30 '13 at 10:39
  • 1
    @Masse: Version please? Unless you have a really old and broken compiler, it should compile fine. – Jesse Good Sep 30 '13 at 10:41
  • I am not sure if that is a deprecated syntax. It used to be allowed which helped automatic code generators. It can also be useful sometimes in versioning where you don't have to modify the line of the last item in the list if you append more items. – CashCow Sep 30 '13 at 10:42
  • Your compiler reports *what* error? – user207421 Sep 30 '13 at 10:43
  • 4.3.0, invalid initialization. – Masse Sep 30 '13 at 10:46
  • GCC 4.7.3 does not produce an error. – juanchopanza Sep 30 '13 at 10:46
  • Thanks all for the links. – Masse Sep 30 '13 at 10:46
  • @juanchopanza: The 'latest' IDE1 also. – Masse Sep 30 '13 at 10:47
  • Re this being marked as a duplicate: it would be nice if the question it duplicates were tagged C or C++. How does one find it otherwise. (It _is_ a duplicate, and the highest voted answer is very good. My only issue is in the tagging of the first question.) – James Kanze Sep 30 '13 at 12:03

2 Answers2

1

Since the possible duplicate question is about programming language syntax in general, here's the answer in C/C++ particularly:

C11(ISO/IEC 9899:201x) §6.7.9 Initialization

Syntax

initializer:

assignment-expression

{ initializer-list }

{ initializer-list , }

I've committed the rest, note the comma here.

In C++, it's similar, note the comma opt, which means an optional comma.

C++11(ISO/IEC 14882:2011) §8.5 Initializers [dcl.init]

A declarator can specify an initial value for the identifier being declared. The dentifier designates an object or reference being initialized. The process of initialization escribed in the remainder of 8.5 applies also to initializations specified by other syntactic contexts, such as the initialization of function parameters with argument expressions (5.2.2) or the initialization of return values (6.6.3).

initializer:

= initializer-clause

( expression-list )

initializer-clause:

assignment-expression

{ initializer-list ,opt }

{ }

initializer-list:

initializer-clause

initializer-list , initializer-clause

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

From ISO/IEC 9899:TC3 6.7.8 Initialization

26 EXAMPLE 3

The declaration

int y[4][3] = {
{ 1, 3, 5 },
{ 2, 4, 6 },
{ 3, 5, 7 },
};

is a definition with a fully bracketed initialization: 1, 3, and 5 initialize the first row of y (the array object y[0]), namely y[0][0], y[0][1], and y[0][2]. Likewise the next two lines initialize y[1] and y[2]. The initializer ends early, so y[3] is initialized with zeros. Precisely the same effect could have been achieved by

int y[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};

The initializer for y[0] does not begin with a left brace, so three items from the list are used. Likewise the next three are taken successively for y[1] and y[2].

So at least for c99 it is absoloutly legal!

dhein
  • 6,431
  • 4
  • 42
  • 74