barry@lizard.metaphor.com (Barry Friedman) (06/29/90)
Excuse me if I am being an ignoramus but... I want to create two enumerated types, one being a subset of the other, something like this: typedef enum { a, b, c, d, e } Set; /* This defines the full set */ typedef enum { b, c, d } SubSet; /* This is a subset of above */ I wasn't too surprised when my compiler choked on this. Is there a way to do this in C? I have checked a few books and I haven't seen anything that hints at a way to do it. Thanks in advance. ======================================================================== Barry_Friedman(); /* "Why stop now, just when I'm hating it?" - Marvin the Paranoid Android */
winans@mcs.anl.gov (John Winans) (06/30/90)
In article <1256@metaphor.Metaphor.COM> barry@lizard.metaphor.com (Barry Friedman) writes: >Excuse me if I am being an ignoramus but... > >I want to create two enumerated types, one being a subset of the >other, something like this: > >typedef enum { a, b, c, d, e } Set; /* This defines the full set */ >typedef enum { b, c, d } SubSet; /* This is a subset of above */ > >I wasn't too surprised when my compiler choked on this. Is there a >way to do this in C? I have checked a few books and I haven't seen >anything that hints at a way to do it. Thanks in advance. My understanding of enumerated types is that (using your example) the a would be psuedo-#defined to 0, the 'b' to 1, 'c' to 2 and so on. If your subset had it's vars in the same position as the set... it would work!! As in: typedef enum { b, c, d, a, e } Set; /* This defines the full set */ typedef enum { b, c, d } SubSet; /* This is a subset of above */ I have no idea if lint would print nasties about this I never use it :-) But if i ever defined an enumerated data type like the above, I most certainly would doc the thing as ordered for a purpose. -- ! John Winans Advanced Computing Research Facility ! ! winans@mcs.anl.gov Argonne National Laboratory, Illinois ! ! ! ! If it weren't for time, everything would happen at once. !
zhu@crabcake.cs.jhu.edu (Benjamin Zhu) (06/30/90)
In article <1990Jun29.223919.3325@mcs.anl.gov> winans@mcs.anl.gov (John Winans) writes: >In article <1256@metaphor.Metaphor.COM> barry@lizard.metaphor.com (Barry Friedman) writes: >>Excuse me if I am being an ignoramus but... >> >>I want to create two enumerated types, one being a subset of the >>other, something like this: >> >>typedef enum { a, b, c, d, e } Set; /* This defines the full set */ >>typedef enum { b, c, d } SubSet; /* This is a subset of above */ >> >>I wasn't too surprised when my compiler choked on this. Is there a >>way to do this in C? I have checked a few books and I haven't seen >>anything that hints at a way to do it. Thanks in advance. > >My understanding of enumerated types is that (using your example) the a would >be psuedo-#defined to 0, the 'b' to 1, 'c' to 2 and so on. If your subset had >it's vars in the same position as the set... it would work!! As in: > >typedef enum { b, c, d, a, e } Set; /* This defines the full set */ >typedef enum { b, c, d } SubSet; /* This is a subset of above */ No, this would not work. Check out K&R (pp 39). Names at different enumeration should be distinct. Please do not take something for granted just because enumerations are quite similar to #define's. As to the original posting, do you have absolute necessity to use the same names in different enumerations at all? I am not aware of any tricks to do this. I probably would get rid of enumerations and use macros instead. Again, I am not an authority on this matter. > >I have no idea if lint would print nasties about this I never use it :-) But if >i ever defined an enumerated data type like the above, I most certainly would >doc the thing as ordered for a purpose. > > >-- >! John Winans Advanced Computing Research Facility ! >! winans@mcs.anl.gov Argonne National Laboratory, Illinois ! >! ! >! If it weren't for time, everything would happen at once. ! Benjamin Zhu +=========================================================================+ +Disclaimer: + + I do not represent Johns Hopkins, Johns Hopkins does not + + represent me either. + +==========================================================================
diamond@tkou02.enet.dec.com (diamond@tkovoa) (06/30/90)
In article <1256@metaphor.Metaphor.COM> barry@lizard.metaphor.com (Barry Friedman) writes: >I want to create two enumerated types, one being a subset of the >other, something like this: >typedef enum { a, b, c, d, e } Set; /* This defines the full set */ >typedef enum { b, c, d } SubSet; /* This is a subset of above */ typedef enum { a, b, c, d, e } Set; /* This defines the full set and more */ typedef Set SubSet; /* This does too */ If you want subranges (such as limiting Set to the range a..e, let alone what you want to do to SubSet), then you have to use another language. -- Norman Diamond, Nihon DEC diamond@tkou02.enet.dec.com This is me speaking. If you want to hear the company speak, you need DECtalk.