473,394 Members | 1,866 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,394 software developers and data experts.

an enum and ntoh question

Hello

If I have an enum definition
for instance

typedef enum {
F0 = 0,
F1 = 1,
F2 = 2,
F3 =3
} Ftest;

Ftest a;
and I need to do ntoh, which one to use ntohl or ntohs?

And do I have to cast it back to Ftest?

Thanks a lot!

Feb 2 '06 #1
13 3039
po***********@gmail.com wrote:
Hello

If I have an enum definition
for instance

typedef enum {
F0 = 0,
F1 = 1,
F2 = 2,
F3 =3
} Ftest;

Ftest a;
and I need to do ntoh, which one to use ntohl or ntohs?

And do I have to cast it back to Ftest?


Your question is off topic, ntoh*() and hton*() functions not being part
of Standard C.

Also, your question does not seem to be too well framed.

What I think you're asking may be what's the `type` of an `enum`. Well,
the Standard specifies that it has to fit in an `int`. Now, look up how
your functions are declared, and you'll figure out (or not) what to do.
It seems to depend on the size of your `int`.

Cheers

Vladimir

--
If only I could be respected without having to be respectable.

Feb 2 '06 #2
po***********@gmail.com wrote:
# Hello
#
# If I have an enum definition
# for instance
#
# typedef enum {
# F0 = 0,
# F1 = 1,
# F2 = 2,
# F3 =3
# } Ftest;
#
# Ftest a;
# and I need to do ntoh, which one to use ntohl or ntohs?

Why guess? Cast it and you don't have to worry. What are you using it for?
Do you need a long or short result?

# And do I have to cast it back to Ftest?

Ftest is just another integer.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
If you plan to shoplift, let us know.
Thanks
Feb 3 '06 #3
SM Ryan wrote:
po***********@gmail.com wrote:
# Hello
#
# If I have an enum definition
# for instance
#
# typedef enum {
# F0 = 0,
# F1 = 1,
# F2 = 2,
# F3 =3
# } Ftest;
#
# Ftest a;
# and I need to do ntoh, which one to use ntohl or ntohs?

Why guess? Cast it and you don't have to worry. What are you using it for?
Do you need a long or short result?

# And do I have to cast it back to Ftest?

Ftest is just another integer.


No, it's not, it's a new type called `Ftest`, which is actually an
`enum`. The fact that enums can be represented as ints (as the Standard
assures us) doe snot mean that enums /are/ ints. They're a completely
different beast.

Cheers

Vladimir

Feb 3 '06 #4

po***********@gmail.com wrote:
Hello

If I have an enum definition
for instance

typedef enum {
F0 = 0,
F1 = 1,
F2 = 2,
F3 =3
} Ftest;

Ftest a;
and I need to do ntoh, which one to use ntohl or ntohs?

And do I have to cast it back to Ftest?

Thanks a lot!


Ftest is in the range [0,3], that fits in an uint16, thus ntohs/htons.

Feb 3 '06 #5
>> po***********@gmail.com wrote:
typedef enum { /* ... */ } Ftest;
SM Ryan wrote:
Ftest is just another integer.

In article <11**********************@o13g2000cwo.googlegroups .com>,
Vladimir S. Oka <no****@btopenworld.com> wrote:No, it's not, it's a new type called `Ftest`, which is actually an
`enum`. The fact that enums can be represented as ints (as the Standard
assures us) doe snot mean that enums /are/ ints. They're a completely
different beast.


Well, more specifically, they are compatible with an integer
type -- we just are not told which one (and compilers can
change "which one" more or less at whim, too).

Peculiarly, however, enumeration *constants* have type "int".

Since the constants have type "int", the only types that really
make sense, as compiler choices, for the enumerated types, are
char, signed char, unsigned char, short, unsigned short, and int.
(Well, in C99, a compiler can use an "extended" type, including
types halfway between char and short for instance -- one might
do this on a Cray, where "char" is 8 bits but "short" is 64.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 3 '06 #6
Chris Torek <no****@torek.net> writes:
[...]
In article <11**********************@o13g2000cwo.googlegroups .com>,
Vladimir S. Oka <no****@btopenworld.com> wrote:
No, it's not, it's a new type called `Ftest`, which is actually an
`enum`. The fact that enums can be represented as ints (as the Standard
assures us) doe snot mean that enums /are/ ints. They're a completely
different beast.
Well, more specifically, they are compatible with an integer
type -- we just are not told which one (and compilers can
change "which one" more or less at whim, too).

Peculiarly, however, enumeration *constants* have type "int".

Since the constants have type "int", the only types that really
make sense, as compiler choices, for the enumerated types, are
char, signed char, unsigned char, short, unsigned short, and int.


Though a sufficiently perverse compiler could make an enumerated type
compatible with signed or unsigned long or long long.
(Well, in C99, a compiler can use an "extended" type, including
types halfway between char and short for instance -- one might
do this on a Cray, where "char" is 8 bits but "short" is 64.)


One probably wouldn't, though; the reasons for "short" being 64 bits
(that the hardware can't directly access quantities smaller than 64
bits) apply equally to enumerated types. (This applies only to *some*
Cray systems, BTW.)

--
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.
Feb 3 '06 #7
"Vladimir S. Oka" wrote:
po***********@gmail.com wrote:
If I have an enum definition
for instance

typedef enum {
F0 = 0,
F1 = 1,
F2 = 2,
F3 =3
} Ftest;

Ftest a;
and I need to do ntoh, which one to use ntohl or ntohs?

And do I have to cast it back to Ftest?
Your question is off topic, ntoh*() and hton*() functions not being part
of Standard C.

Also, your question does not seem to be too well framed.

What I think you're asking may be what's the `type` of an `enum`. Well,
the Standard specifies that it has to fit in an `int`.


While that's true, there's a subtlety involved: a specific enumeration
type may be stored in a implementation-defined compatible integer type
smaller than an int. It then has the same rank as the compatible
integer type.
Now, look up how
your functions are declared, and you'll figure out (or not) what to do.
It seems to depend on the size of your `int`.


Not necessarily. A specific enumeration type may stored in a short.
The value of the variable should be limited to range of values in the
defined members of the type.

--
Thad
Feb 4 '06 #8
On 2006-02-04, Thad Smith <Th*******@acm.org> wrote:
"Vladimir S. Oka" wrote:
po***********@gmail.com wrote:
> If I have an enum definition
> for instance
>
> typedef enum {
> F0 = 0,
> F1 = 1,
> F2 = 2,
> F3 =3
> } Ftest;
>
> Ftest a;
> and I need to do ntoh, which one to use ntohl or ntohs?
>
> And do I have to cast it back to Ftest?


Your question is off topic, ntoh*() and hton*() functions not being part
of Standard C.

Also, your question does not seem to be too well framed.

What I think you're asking may be what's the `type` of an `enum`. Well,
the Standard specifies that it has to fit in an `int`.


While that's true, there's a subtlety involved: a specific enumeration
type may be stored in a implementation-defined compatible integer type
smaller than an int. It then has the same rank as the compatible
integer type.


What if there are more than INT_MAX+1 items in the enumeration? Is an
implementation required to reject these, or may it choose long as the
integer type? Or is there undefined behavior?
Feb 5 '06 #9
Jordan Abel wrote:
What if there are more than INT_MAX+1 items in the enumeration? Is an
implementation required to reject these, or may it choose long as the
integer type? Or is there undefined behavior?


No, an implementation is not required to reject a declaration with
more than INT_MAX+1 enumeration constants. If the number of declared
enumeration constants exceeds the translator limit, a diagnostic
/should/ be issued (sorry, no C&V).

An implementation must support at least 1023 enumeration constants
(items) for a single enumeration. It may support more. Because there
may be separate enumeration constants with the same value and
enumeration constants can have assigned values, the number of
enumeration constants in an enumeration type is independent of the
range of values.

Although enumeration constants have type int, a specific enumeration
type may have a smaller range of values, be stored in a smaller int
type, and have a lesser rank. Standard C only supports enumeration
values within the range for type int. Attempting to specify an
enumeration constant with a value outside the range of an int violates
a constraint and requires a diagnostic. A conforming implementation
could still support larger values and provide sufficient storage for
the declared range, but it must still issue the diagnostic (in
conforming mode).

--
Thad
Feb 5 '06 #10
Jordan Abel wrote:
.... snip ...
What if there are more than INT_MAX+1 items in the enumeration?
Is an implementation required to reject these, or may it choose
long as the integer type? Or is there undefined behavior?


Ridiculous. That would mean that someone, somewhere, spend
considerable time typing in at least 32767 unique identifiers into
the enum definition statement. The result would exceed the
standard guaranteed size availability for compiler input lines.

Don't worry your pretty little head about it.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Feb 5 '06 #11
Jordan Abel <ra*******@gmail.com> writes:
On 2006-02-04, Thad Smith <Th*******@acm.org> wrote:

[snip]
While that's true, there's a subtlety involved: a specific enumeration
type may be stored in a implementation-defined compatible integer type
smaller than an int. It then has the same rank as the compatible
integer type.


What if there are more than INT_MAX+1 items in the enumeration? Is an
implementation required to reject these, or may it choose long as the
integer type? Or is there undefined behavior?


C99 6.7.2.2p2 (a constraint) says:

The expression that defines the value of an enumeration constant
shall be an integer constant expression that has a value
representable as an int.

6.7.2.2p3 says:

The identifiers in an enumerator list are declared as constants
that have type int and may appear wherever such are permitted.

I don't see an explicit statement about this case:

#include <limits.h>
enum { foo = INT_MAX, bar };

where an enumerator has a value not representable as an int, but
there's no expression defining it -- but it seems reasonable to assume
that it's an error. (It's undefined behavior at the very least.)

--
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.
Feb 5 '06 #12
CBFalconer <cb********@yahoo.com> writes:
Jordan Abel wrote:

... snip ...

What if there are more than INT_MAX+1 items in the enumeration?
Is an implementation required to reject these, or may it choose
long as the integer type? Or is there undefined behavior?


Ridiculous. That would mean that someone, somewhere, spend
considerable time typing in at least 32767 unique identifiers into
the enum definition statement. The result would exceed the
standard guaranteed size availability for compiler input lines.


Not at all. C code doesn't have to be typed manually; in fact,
automatically generated C code is quite common. And there's no reason
a huge enumeration type has to be declared on a single line.

#include <stdio.h>
int main(void)
{
long i;
printf("enum really_big {\n");
for (i = 0; i <= 32768; i ++) {
printf(" x%05d,\n", i);
}
printf("}\n");
return 0;
}

And of course you can achieve the same effect by specifying values for
the enumerators:

enum not_quite_as_big {
x00000 = 0,
x32768 = 32768,
};

The latter is a constraint violation if INT_MAX==32767; see my
response elsethread for chapter and verse.

--
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.
Feb 5 '06 #13

"Keith Thompson" <ks***@mib.org> wrote
Ridiculous. That would mean that someone, somewhere, spend
considerable time typing in at least 32767 unique identifiers into
the enum definition statement. The result would exceed the
standard guaranteed size availability for compiler input lines.


Not at all. C code doesn't have to be typed manually; in fact,
automatically generated C code is quite common. And there's no reason
a huge enumeration type has to be declared on a single line.

But the whole point of an enum is to make code humanly readable.

Granted, sometimes automatically generated code is designed to be read, but
if it has 32767 types in an enumeration?
Feb 5 '06 #14

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

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
10
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
3
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.