Author |
Message |
Tempora #1 / 6
|
 'C' enum syntax problem
I seem to have some problem in using the enum construct on Sun-3 m/c. The following declarations generate an error. typedef enum WEEK { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } ; typedef enum WEEK_END { SATURDAY, SUNDAY } ; The error messages are redeclaration of SATURDAY redeclaration of SUNDAY Does it mean that subsets of an enum cannot be defined ? Is this a bug in my C Compiler ? What does PAns say about this ? Is there a work around ? Krishna
|
Thu, 17 Sep 1992 17:05:00 GMT |
|
 |
Gregory Kemni #2 / 6
|
 'C' enum syntax problem
Quote:
> I seem to have some problem in using the enum construct on Sun-3 > m/c. > The following declarations generate an error. > typedef enum WEEK { > MONDAY, TUESDAY, WEDNESDAY, > THURSDAY, FRIDAY, > SATURDAY, SUNDAY > } ; > typedef enum WEEK_END { > SATURDAY, SUNDAY > } ; > The error messages are > redeclaration of SATURDAY > redeclaration of SUNDAY > Does it mean that subsets of an enum cannot be defined ? > Is this a bug in my C Compiler ?
Lots of C compilers just turn enums into things that are essentially #defines (and barf on them when the symbol is reused). Another thing that won't work is typedef enum WEEK { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } ; ... WEEK workweek; switch (workweek) { case MONDAY: ... case TUESDAY: ... .... Lots of compilers will barf in the switch(), unless you use this switch ((int) workweek) { which sucks. Quote: > Is there a work around ?
The easiest and most elegant workaround (and it always seems to work) is #define MONDAY 0 #define TUESDAY 1 ... typedef short WEEK typedef short WEEKEND Then switches, etc will work fine. Greg Kemnitz
|
Thu, 17 Sep 1992 05:03:00 GMT |
|
 |
Daniel Elba #3 / 6
|
 'C' enum syntax problem
Quote:
>I seem to have some problem in using the enum construct on Sun-3 >The following declarations generate an error. > typedef enum WEEK { MONDAY, TUESDAY, SATURDAY, SUNDAY }; > typedef enum WEEK_END { SATURDAY, SUNDAY };
The identifiers of the enum are, in effect, integral constants. You can use MONDAY, TUESDAY, etc. as though they were manifest constants, assign them to int's (casting may be necessary), and just generally go wild. Because of this, the names of the constants may not be reused in the same scope. You might try something like this: enum WEEK { MONDAY, TUESDAY, SATURDAY, SUNDAY }; enum WEEK_END { Saturday = SATURDAY, Sunday = SUNDAY }; unless, of course, your implementation of C is case-insensitive. -- Not responsible : uunet - for this posting : tektronix \!oresoft!dan
: sun!nosun -
|
Thu, 17 Sep 1992 17:42:00 GMT |
|
 |
Guy Harr #4 / 6
|
 'C' enum syntax problem
Quote: >Does it mean that subsets of an enum cannot be defined ?
It means that the names of enumeration members are global, not local to the "enum" in which they're used; this means that you can't have the same "enum" name belong to two different enumerations (at least not with different values) within the same scope. Since C has no notion of a subrange type, and since you seem to be trying to create a subrange type of WEEK, the answer is basically "no, subranges of an enum cannot be defined." Quote: >Is this a bug in my C Compiler ?
No. It is doing the correct thing. Quote: >What does PAns say about this ?
3.5.2.2 Enumeration specifiers ... Semantics The identifiers in an enumerator list are declared as constants that have type "int" and may apper wherever such are 58 permitted. ... 58. Thus, the identifiers of enumeration constants declared in the same scope shall all be distinct from each other and from other identifiers declared in ordinary declarators. Quote: >Is there a work around ?
typedef enum WEEK { WEEK_MONDAY, WEEK_TUESDAY, WEEK_WEDNESDAY, WEEK_THURSDAY, WEEK_FRIDAY, WEEK_SATURDAY, WEEK_SUNDAY }; typedef enum WEEK_END { WEEK_END_SATURDAY, WEEK_END_SUNDAY }; is one possibility.
|
Thu, 17 Sep 1992 18:23:00 GMT |
|
 |
Shankar Un #5 / 6
|
 'C' enum syntax problem
Quote: > > typedef enum WEEK { > > MONDAY, TUESDAY, WEDNESDAY, > > THURSDAY, FRIDAY, > > SATURDAY, SUNDAY > > } ; > > typedef enum WEEK_END { > > SATURDAY, SUNDAY > > } ; > > redeclaration of SATURDAY > > redeclaration of SUNDAY > > Does it mean that subsets of an enum cannot be defined ? > > Is this a bug in my C Compiler ? > Lots of C compilers just turn enums into things that are essentially > #defines (and barf on them when the symbol is reused).
That is not the reason why it barfs. The reason why it complains is that that piece of code is illegal. Period. While enum's are not quite like #defines, the way that the enumerated constants behave certainly makes it look that way. each occurrence of an identifier inside an enum definition is a definition of that identifier. Thus, in your example, SATURDAY is defined twice, once with a value of 5 and once with a value of 0. What you are trying to do is something like the Pascal construct: type week = (monday, tuesday, ..., sunday); weekend = saturday .. sunday; Where weekend is essentially a subtype of week. So instead of trying a literal translation, try one based on the intent. You want WEEK_END to be a subtype of WEEK? No problem: typedef enum WEEK WEEK; typedef enum WEEK WEEK_END; And use it as WEEK day = MONDAY; WEEKEND holiday = SATURDAY; What, you want range checking? HAHAHAHAH!!!!! ---- Shankar.
|
Thu, 17 Sep 1992 01:00:00 GMT |
|
 |
Clayton Cram #6 / 6
|
 'C' enum syntax problem
# # Does it mean that subsets of an enum cannot be defined ? # # Is this a bug in my C Compiler ? # # Lots of C compilers just turn enums into things that are essentially # #defines (and barf on them when the symbol is reused). # Another thing that won't work is # # typedef enum WEEK { # MONDAY, TUESDAY, WEDNESDAY, # THURSDAY, FRIDAY, # SATURDAY, SUNDAY # } ; # # WEEK workweek; # # switch (workweek) { # case MONDAY: ... # case TUESDAY: ... # # .... # # Lots of compilers will barf in the switch(), unless you use this # # switch ((int) workweek) { # # which sucks. # # # Is there a work around ? # # The easiest and most elegant workaround (and it always seems to work) is # # #define MONDAY 0 # #define TUESDAY 1 # ... # # typedef short WEEK # typedef short WEEKEND # # Then switches, etc will work fine. # # Greg Kemnitz ANSI compilers (both of them!) know that enums are ints, and don't require type casting to use them as ints. There are two great advantages to using enums rather than #defines: 1. Type checking. 2. De{*filter*} symbols. In particular, dbx lets you see your enum variable as _Tuesday_, not 2, which greatly simplifies debugging -- especially when you have 219 items in the enumerated type. (Unfortunately, Microsoft CodeView, for all its other virtues, doesn't know about enumerated types -- it doesn't even give you a number -- just tells you it can't display the value. That's DUMB!) Of course, if portability to antique non-ANSI compilers is important, you may want to kludge in #defines. -- Clayton E. Cramer {pyramid,pixar,tekbspa}!optilink!cramer Governments that don't trust most people with weapons, deserve no trust. ---------------------------------------------------------------------------- Disclaimer? You must be kidding! No company would hold opinions like mine!
|
Thu, 17 Sep 1992 17:02:00 GMT |
|
|
|