ColonelHedgehog ColonelHedgehog - 1 month ago 16
C Question

Enums defined with array constants

Is it possible to define enum constants with arrays? Like this:

enum myEnum
{
abc = {12, 34, 56};
def = {-12, -13, -14};
};

Answer

This is not possible. An enum constant must be an integer type.

From section 6.7.2.2 of the C standard:

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

3 The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. 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. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.