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

B

Bart Simpson

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 ?
 
V

Vladimir S. Oka

Bart said:
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.
 
K

Keith Thompson

Vladimir S. Oka said:
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
};
 
V

Vladimir S. Oka

Keith said:
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?
 
S

Sandeep

Vladimir said:
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.
 
K

Kenneth Brody

Vladimir S. Oka said:
Keith said:
Vladimir S. Oka said:
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: <mailto:[email protected]>
 
M

Michael Mair

Vladimir said:
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
 
M

Michael Mair

Kenneth said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top