[comp.lang.c] { enumeration-list , }

rbutterworth@watmath.waterloo.edu (Ray Butterworth) (03/21/88)

In article <680@kuling.UUCP>, martin@kuling.UUCP (Erik Martin) writes:
> Then, why isn't an 'optional' comma allowed after the last constant in an
> enumeration list? I bumbed into this fact when I needed to generate both
> an initialized array and an enumeration declaration from a file of symbols,
> and found that I had to give special treatment to the last item in the
> enumeration. *Very* frustrating.

Yes, it's frustrating and inconsistent.
However, when I make an enumeration list, I tend to give the last
one a special name.  e.g.
    typedef enum {
        COL_RED,
        COL_GREEN,
        ... ,
        COL_PINK,
        COL_DIMENSION
    } Colour;
The ?_DIMENSION name is always last, for all the enumeration types,
and it never has a comma after it.  The ?_DIMENSION is handy if one
wants to allocate an array, e.g.
    auto Table *table = (Table *)malloc(COL_DIMENSION*sizeof(Table));
or
    auto Table table[COL_DIMENSION];
    auto int index = (int)COL_DIMENSION;
    while (--index >= 0) {
        blah blah table[index] blah blah;
    }
or if you suspect that someone gave you a bad function argument,
    if ((unsigned int)arg >= (unsigned int)COL_DIMENSION) oops();