Public Group

# simple enum question...

This topic is 5031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, simple question about enum's. lets say i do the following: enum { MESG1 = 0, MESG2 = 1 }; this is an anonymous enum then? theres one thing that i dont understand... what is the exact size of an enum? im thinking the compiler determines it based on how many enum's are in the set, but, im not sure, im getting a very strange problem with this in a simpe console chat program using rakknet. basically, i'll do something like this: packet.WriteToPacket(MESG1); packet.WriteToPacket("Hello thar"); packet.Send(); now, on the receving end, the message is never receieved. however, if i do the following: unsigned char msgid = MESG1; packet.WriteToPacket(msgid); packet.WriteToPacket("Hello thar"); packet.Send(); it works! so, why do i have to cast it to an unsigned char? thanks for any help.

##### Share on other sites
what is the exact size of an enum?

The size of the integral type that is necessary to represent the enum's largest element. Here, it should be char-sized.

What parameter type does packet.WriteToPacked() expect?

##### Share on other sites
it is over-loaded, and can take bool, char, short, long, double, float, int (isnt int and long the same thing???),and a char * w/ len of string.

so, any idea why this is happening? theres about 50 or so enum's in the set, so this should make it a signed char then, right?

thanks for anymore help.

##### Share on other sites
isnt int and long the same thing???

They may have the same size on your platform, but they are two different types. Similarly, you do have a char type which may, in practice, be signed or unsigned, but is neither unsigned char nor signed char.

(note that, in C, enums are all int-sized, IIRC)

so, any idea why this is happening?

Yes.

theres about 50 or so enum's in the set, so this should make it a signed char then, right?

No. It may have the same size, but it is not a signed char - it is an (anonymous) enum. On my system, a quick test

#include <stdio.h>enum { FOO = 0 };#define DFOO(x) void foo(x) { puts(#x); }DFOO(bool);DFOO(char);DFOO(short);DFOO(long);DFOO(double);DFOO(float);DFOO(int);DFOO(char*);int main(){   foo(FOO);}

Shows it to convert to int (and I am to tired to check the Holy Standard tonight, rating++ (if possible) to whomever quotes the appropriate verses).

##### Share on other sites
thanks a lot Fruny... but, i dont understand, if it *should* only be the largest needed to represent my highest number, then why isn't it a char ? im getting the int printout too BTW.

##### Share on other sites
I use bitstreams with RakNet and cast all my enums into ints. Works allright, if your interested in going that route.

You should ask this question here too, if you haven't already. Kevin provides great support for his library.

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5
gaxio
13

• 24
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631734
• Total Posts
3001933
×