473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3045
po***********@g mail.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***********@g mail.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***********@g mail.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***********@g mail.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***********@g mail.com wrote:
typedef enum { /* ... */ } Ftest;
SM Ryan wrote:
Ftest is just another integer.

In article <11************ **********@o13g 2000cwo.googleg roups.com>,
Vladimir S. Oka <no****@btopenw orld.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.n et> writes:
[...]
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
Vladimir S. Oka <no****@btopenw orld.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_Keit h) 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***********@g mail.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***********@g mail.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

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

Similar topics

20
4809
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 EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
10
5775
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 TOKEN t2 = 5; // compile error (cannot convert from
4
6477
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? Thanks in advance... -Nikhil
5
2656
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 references the Data layer, but I would like to have the enum exposed to all three. Is there a way to trickle down that enum (Like class inheritance...
6
2866
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 understand correctly, what is actually happening on the calling side is an instance of the enum class FavoriteSport is getting created and sent to the...
13
12367
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
3898
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 want to refer to it in an class I get the following error: cannot expose type outside the project through class
34
11150
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a...
4
1932
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 writing a program where i will have an enum in one of my classes. When the program is used, the user will define what actually goes inside the enum. So for...
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.