473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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 ?

Mar 10 '06 #1
8 5810

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

Mar 10 '06 #2
"Vladimir S. Oka" <no****@btopenworld.com> 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) ks***@mib.org <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.
Mar 10 '06 #3

Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenworld.com> 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

Mar 10 '06 #4
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.

Mar 10 '06 #5
Vladimir S. Oka schrieb:
Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenworld.com> 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.
Mar 10 '06 #6
"Vladimir S. Oka" wrote:

Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenworld.com> 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: <mailto:Th*************@gmail.com>

Mar 10 '06 #7
Kenneth Brody <ke******@spamcop.net> 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;}
Mar 10 '06 #8
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.
Mar 10 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
9
by: Jarmo Muukka | last post by:
Hello all! I have used C++ for many years and recently I have used C#. Now I am coding in C++ and I have a problem now. In C# you can do enums like this: enum Lane { Left,
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
0
by: Jason Coyne | last post by:
Here is another programming blog entry. Sorry for the normal friends :) I recently ran into a problem with a web service I was trying to call from c# where the web service returned null dates...
37
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. ...
10
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
I came over this code which puzzled me. Isn't the enum supposed to have an identifier? enum { BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ BT_OPEN, BT_BOUND,...
2
by: Jason Kraftcheck | last post by:
If I have a struct defined as follows: enum ABCD { A = 0, B = 1, C = 2, D = 3 }; struct Foo { ABCD val : 2; } and I do: Foo f; f.val = C; ABCD v = f.val;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.