问题
I need to allocate an array according to how many elements the enum
have. I did the following:
enum { A, B, C, LAST };
char buf[LAST];
That works fine,even with -ansi -pedantic
flags. But I'm not sure if it's a GCC or clang(wich supports most,if not all GCC-extensions) extensions or really allowed by the ANSI C standard and will works fine in any C compiler with ANSI-C std. Can someone clarify it?
回答1:
Both the C89 (section 3.5.2.2) and C99 (section 6.7.2.2) standards define enums the same way:
6.7.2.2 Enumeration specifiers (Paragraph 3), http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
3.5.2.2 Enumeration specifiers (Paragraph 3), http://flash-gordon.me.uk/ansi.c.txt
Both read:
[...] An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =,the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. [...]
Therefore, in your syntax, any standard-compliant compiler will run your code correctly.
回答2:
That works fine,even with -ansi -pedantic flags
So it's not a GNU extension. Yes, this is fine in ANSI C, because members of an enum
are constant expressions.
回答3:
As others have said, it is valid. But I think no one has quoted the right sections so far. The relevant ones from the N1256 C99 draft are: 6.6 "Constant expressions" paragraph 6:
An integer constant expression99) shall have integer type and shall only have operands that are integer constants, enumeration constants [...]
and then 6.7.5.2 "Array declarators" paragraph 4:
If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type [...]
So basically:
- enumeration constants are constant expressions
- for the array not to be variable length, we need a constant expression
I believe that 6.7.2.2 "Enumeration specifiers" which others quoted talks about declaring the enum
, not using the enumerators. Of course, since when declaring them you need compile time constants, we expect that they should also be compile time constants when used in expressions.
回答4:
Can someone clarify it?
I'm sure you know, an enum is just appling a label to a number:
enum
{ A, // 0
B, // 1
C, // 2
LAST // 3
};
So really:
char buf[LAST];
Is no different than:
char buf[3];
回答5:
From C Standard, paragraph 6.2.5 (Types):
16 An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type.
17 The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types.
Also, paragraph 6.7.2.2 (Enumeration specifiers):
The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.
来源:https://stackoverflow.com/questions/13770314/can-enum-member-be-the-size-of-an-array-in-ansi-c