简体   繁体   English

枚举成员可以是ANSI-C中数组的大小吗?

[英]Can enum member be the size of an array in ANSI-C?

I need to allocate an array according to how many elements the enum have. 我需要根据enum有多少个元素来分配一个数组。 I did the following: 我做了以下事情:

enum { A, B, C, LAST };
char buf[LAST];

That works fine,even with -ansi -pedantic flags. 即使使用-ansi -pedantic标志,也可以正常工作。 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. 但是我不确定它是GCC还是clang(如果不支持所有GCC扩展,则支持大多数扩展)还是ANSI C标准真正允许的扩展,是否可以在具有ANSI-C std的任何C编译器中正常工作。 Can someone clarify it? 有人可以澄清吗?

Both the C89 (section 3.5.2.2) and C99 (section 6.7.2.2) standards define enums the same way: C89(第3.5.2.2节)和C99(第6.7.2.2节)标准对枚举的定义方式相同:

6.7.2.2 Enumeration specifiers (Paragraph 3), http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf 6.7.2.2枚举规范 (第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 3.5.2.2枚举说明符 (第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. 如果第一个枚举数不为=,则其枚举常数的值为0。每个后续的枚数不等于=的枚举常数将其枚举常数定义为将前一个枚举常数的值加1所得的常数表达式的值。 [...] [...]

Therefore, in your syntax, any standard-compliant compiler will run your code correctly. 因此,使用您的语法,任何符合标准的编译器都将正确运行您的代码。

That works fine,even with -ansi -pedantic flags 即使使用-ansi -pedantic标志也可以正常工作

So it's not a GNU extension. 因此,它不是GNU扩展。 Yes, this is fine in ANSI C, because members of an enum are constant expressions. 是的,这在ANSI C中很好,因为enum成员是常量表达式。

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: N1256 C99草案中的相关内容是:6.6“常量表达式”第6段:

An integer constant expression99) shall have integer type and shall only have operands that are integer constants, enumeration constants [...] 整数常量表达式99)应该具有整数类型,并且只能具有整数常量,枚举常量[...]的操作数

and then 6.7.5.2 "Array declarators" paragraph 4: 然后是6.7.5.2“数组声明符”第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. 我相信其他人引用的6.7.2.2“枚举说明符”谈论的是声明enum ,而不是使用枚举。 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. 当然,由于声明它们时需要编译时间常数,因此我们希望它们在表达式中使用时也应该是编译时间常数。

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];

From C Standard, paragraph 6.2.5 (Types): 根据C标准第6.2.5节(类型):

16 An enumeration comprises a set of named integer constant values. 16枚举包括一组命名的整数常数值。 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. 17 char类型,有符号和无符号整数类型以及枚举类型统称为整数类型。

Also, paragraph 6.7.2.2 (Enumeration specifiers): 另外,第6.7.2.2段(枚举说明符):

The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int. 定义枚举常量值的表达式应为整数常量表达式,其值可表示为int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM