I have an enumeration in C as follows:
enum menu
{
PASTA,
PIZZA,
DIET_COKE,
MOJITO,
};
Since I haven’t explicitly mentioned the integer values corresponding to these elements, they are assigned values 0,1,2, and 3 respectively.
Say I decide to add another 100 or so items to my enum.
Then is there a way to access these elements by the number they are associated with?
(Like how we can access an element of an array using its index)
>Solution :
The enums are nothing but named constants in C. Same as you declare a const using
#define PASTA 0
#define PIZZA 1
#define DIET_COKE 2
#define MOJITO 3
for example.
With the enum the compiler does that for you automatically. So there is no way to access enums in a way you want in C, unless you create an array for them.