struct foo {
enum { MAXELEMENTS=10 };
long id[MAXELEMENTS];
};
enum hack like this is valid in C (But not a good style of code, see @Jonathan Leffler's comment). Because when id
is defined, MAXELEMENTS
qualifies with enumeration constant.
C99 6.2.1 Scopes of identifiers
Structure, union, and enumeration tags have scope that begins just after the appearance of
the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.
And an enumeration is a type of integer constant.
C99 6.6 Constant expressions
An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.
Finally, an integer constant can be used in array declarators, see C99 6.7.5.2 Array declarators .That's not surprising.