Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this legal ? (non-contiguous values for enums)

Reply
Thread Tools

Is this legal ? (non-contiguous values for enums)

 
 
Bart Simpson
Guest
Posts: n/a
 
      03-10-2006
typedef enum {
ACCESS_DENIED = 1,
ACCESS_READ_ONLY = 2,
ACCESS_READ_WRITE = 4
}AccessTypeEnum ;

Is this legal? - can I have 'gaps' in the enumeration integer values as
above ?

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-10-2006

Bart Simpson wrote:
> typedef enum {
> ACCESS_DENIED = 1,
> ACCESS_READ_ONLY = 2,
> ACCESS_READ_WRITE = 4
> }AccessTypeEnum ;
>
> Is this legal? - can I have 'gaps' in the enumeration integer values as
> above ?


Yes.

If you don't provide any values, they start at 0. Between any two
values you provide, the ones without explicit values are incremented by
1 starting from the last one specified.

--
BR, Vladimir

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-10-2006
"Vladimir S. Oka" <(E-Mail Removed)> writes:
> Bart Simpson wrote:
>> typedef enum {
>> ACCESS_DENIED = 1,
>> ACCESS_READ_ONLY = 2,
>> ACCESS_READ_WRITE = 4
>> }AccessTypeEnum ;
>>
>> Is this legal? - can I have 'gaps' in the enumeration integer values as
>> above ?

>
> Yes.
>
> If you don't provide any values, they start at 0. Between any two
> values you provide, the ones without explicit values are incremented by
> 1 starting from the last one specified.


You can even have repeated values:

enum foo {
foo_first = 0,
zero = 0,
one = 1,
two = 2,
foo_last = 2
};

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-10-2006

Keith Thompson wrote:
> "Vladimir S. Oka" <(E-Mail Removed)> writes:
> > Bart Simpson wrote:
> >> typedef enum {
> >> ACCESS_DENIED = 1,
> >> ACCESS_READ_ONLY = 2,
> >> ACCESS_READ_WRITE = 4
> >> }AccessTypeEnum ;
> >>
> >> Is this legal? - can I have 'gaps' in the enumeration integer values as
> >> above ?

> >
> > Yes.
> >
> > If you don't provide any values, they start at 0. Between any two
> > values you provide, the ones without explicit values are incremented by
> > 1 starting from the last one specified.

>
> You can even have repeated values:
>
> enum foo {
> foo_first = 0,
> zero = 0,
> one = 1,
> two = 2,
> foo_last = 2
> };


I didn't know that. Do you have an example of how it can be useful?

--
BR, Vladimir

 
Reply With Quote
 
Sandeep
Guest
Posts: n/a
 
      03-10-2006
Vladimir S. Oka wrote:

> > You can even have repeated values:
> >
> > enum foo {
> > foo_first = 0,
> > zero = 0,
> > one = 1,
> > two = 2,
> > foo_last = 2
> > };

>
> I didn't know that. Do you have an example of how it can be useful?


A possible Usage :

enum err_codes
{
err_foo = -1,
err_bar = -1,
err_success = 0
};

For clarity , you might want to provide named constants with same value
but different names. The above enum can be used to return the error
code using different name.

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      03-10-2006
"Vladimir S. Oka" wrote:
>
> Keith Thompson wrote:
> > "Vladimir S. Oka" <(E-Mail Removed)> writes:
> > > Bart Simpson wrote:
> > >> typedef enum {
> > >> ACCESS_DENIED = 1,
> > >> ACCESS_READ_ONLY = 2,
> > >> ACCESS_READ_WRITE = 4
> > >> }AccessTypeEnum ;
> > >>
> > >> Is this legal? - can I have 'gaps' in the enumeration integer values as
> > >> above ?
> > >
> > > Yes.

[...]
> > You can even have repeated values:
> >
> > enum foo {
> > foo_first = 0,
> > zero = 0,
> > one = 1,
> > two = 2,
> > foo_last = 2
> > };

>
> I didn't know that. Do you have an example of how it can be useful?


enum item_list {
item_lowest = 0,
item_foo = 0,
item_bar = 1,
item_foobar = 2,
item_highest = 2
};

You could then, for example, check a list with a for loop going from
"item_lowest" to "item_highest", and not have to worry about adding
additional items in the future.


Question: can an enum entry be based on another entry of the same enum?
For example:

enum item_list {
item_lowest,
item_foo = item_lowest,
item_bar,
item_foobar,
item_highest = item_foobar
};

(This "works" on my system, but we all know how reliable "it works on
my system" is for generalizations.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      03-10-2006
Vladimir S. Oka schrieb:
> Keith Thompson wrote:
>
>>"Vladimir S. Oka" <(E-Mail Removed)> writes:
>>
>>>Bart Simpson wrote:
>>>
>>>>typedef enum {
>>>> ACCESS_DENIED = 1,
>>>> ACCESS_READ_ONLY = 2,
>>>> ACCESS_READ_WRITE = 4
>>>>}AccessTypeEnum ;
>>>>
>>>>Is this legal? - can I have 'gaps' in the enumeration integer values as
>>>>above ?
>>>
>>>Yes.
>>>
>>>If you don't provide any values, they start at 0. Between any two
>>>values you provide, the ones without explicit values are incremented by
>>>1 starting from the last one specified.

>>
>>You can even have repeated values:
>>
>>enum foo {
>> foo_first = 0,
>> zero = 0,
>> one = 1,
>> two = 2,
>> foo_last = 2
>>};

>
> I didn't know that. Do you have an example of how it can be useful?


If you have logical subranges in the enumeration constants,
then you often find the equivalent of the above, i.e.

enum foo {
EFooInvalid,

EFooErrStart,
EFooErrBar = EFooErrStart,
....
EFooErrBaz,
EFooErrEnd = EFooErrBaz,

EFooWarnStart,
EFooWarnQux = EFooWarnStart,
....

EFooSuccessStart,
EFooOK = EFooSuccessStart,
EFooFullSuccess,
EFooSuccessEnd = EFooFullSuccess,

EFooMaxReturn = ESuccessEnd,

EFooNumReturnCodes
};

int isFooWarningReturn (enum foo retval)
{
return (EFooWarnStart <= retval)
&& (retval <= EFooWarnEnd);
}

I usually use separate values for the "Ends".
This can make sense for bitranges, too.
E.g.
enum FooOffsets {
EFoStartWobble = 0,
EFoIsRoundWobble = 0,
...
EFoWobbleTypeBit3,
EFoEndWobble,

EFoStartWibble = EFoEndWobble,
EFoIsSquareWibble = EFoStartWibble,
....
EFoEndWibble,
....
};

#define CONCAT(a, b) a##b
#define FOO_MASK(WKind) ((1 << CONCAT(EFoEnd, WKind)) \
- (1 << CONCAT(EFoEnd, WKind)))

These are obviously made-up but should give an impression
of what you can abuse this for

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-10-2006
Kenneth Brody <(E-Mail Removed)> writes:

> Question: can an enum entry be based on another entry of the same enum?


Yes (as long as dependents follow dependencies).
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      03-10-2006
Kenneth Brody schrieb:
> Question: can an enum entry be based on another entry of the same enum?
> For example:
>
> enum item_list {
> item_lowest,
> item_foo = item_lowest,
> item_bar,
> item_foobar,
> item_highest = item_foobar
> };
>
> (This "works" on my system, but we all know how reliable "it works on
> my system" is for generalizations.)


Yep. An enumeration constant is "visible" as soon as it is declared.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not open Design view. Quote values differently in a '<%..."values"...%>' block. Rob Meade ASP .Net 2 08-10-2004 12:46 PM
Can you persist table cell values like you do with form field values? Richard Dixson ASP .Net 2 05-18-2004 01:11 PM
Can you persist table cell values like you can do with form field values? Richard Dixson ASP .Net 1 05-18-2004 03:47 AM
Comparing values in 2 textfiles and returning the missing values Jorgen Gustafsson Perl 4 12-12-2003 09:09 AM
Re: Config values or Application values - which is better/faster? S. Justin Gengo ASP .Net 2 08-20-2003 04:08 PM



Advertisments