typedef question

C

C_guy

I noticed that

typedef enum _myEnum

{

enum1 = 0,
enum2,
enum3,
.....
} myEnum_t;

is making myEnum_t a 4-byte type.

How can I get the same "enumerate" feature, but make myEnum_t a 2-byte
type (preferable unsigned short)?

Thanks!
 
C

C_guy

Thanks for the reply Rick.

Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the
"enumeration" feature? In other words, enum1/enum2/enum3/enumN will
all be set equal to some value, but the type myEnum_t will be a 2-byte
type...

Thanks!
 
C

C_guy

Thanks for the reply Jensen.

Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the
"enumeration" feature? In other words, enum1/enum2/enum3/enumN will
all be set equal to some value, but the type myEnum_t will be a 2-byte
type...

Thanks!
 
B

Bartc

C_guy said:
Thanks for the reply Jensen.

Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT the
"enumeration" feature? In other words, enum1/enum2/enum3/enumN will
all be set equal to some value, but the type myEnum_t will be a 2-byte

Why not just declare:

short int a;

....
a=enum1;

etc.?
 
K

Keith Thompson

C_guy said:
I noticed that

typedef enum _myEnum

It's inadvisable to use identifiers starting with underscores. Some
of them are reserved for the implementation; others are reserved but
only in certain contexts.

If you insist on using a typedef, you can use the same identifier for
the typedef and the enum tag -- or you can drop the enum tag
altogether:

typedef enum { ... } myEnum_t;

(And I think the _t suffix is reserved by POSIX.)
{

enum1 = 0,
enum2,
enum3,
....
} myEnum_t;

is making myEnum_t a 4-byte type.

How can I get the same "enumerate" feature, but make myEnum_t a 2-byte
type (preferable unsigned short)?

Why do you want to do this?

I don't mean to imply that you don't have a perfectly good reason, but
knowing what that reason is would help us to help you find a solution
to your underlying problem.

Keep in mind that the enumeration constants enum1 et al are not of
type enum; they're of type int. Since the language doesn't tie the
enumerated type to the constants, you don't have to either. Assuming
that unsigned short is 2 bytes (this is *not* guaranteed), you can do:

typedef unsigned short myEnum_t;
enum { enum1 = 0, enum2, enum3 };
 
C

CBFalconer

C_guy said:
Does anyone know if I can make "myEnum_t" a 2-byte type WITHOUT
the "enumeration" feature? In other words, enum1/enum2/enum3/enumN
will all be set equal to some value, but the type myEnum_t will be
a 2-byte type...

You failed to quote the original, so this answer may not be really
responsive.

Define the type as a short (or even as unsigned (or signed) char if
you prefer). Then define the values you will use in an enum. BTW,
don't use leading underscores in names, those names are reserved.

enum x {enum0, enum1, enum2, enum3, enumN = 123};

typedef short myEnum_t;

myEnum_t var1, varx, vary; /* these have the size of a short */

....

var1 = enum1; /* will have the value 1 */
varx = enum3; /* will have the value 3 */
vary = enumN; /* will have the value 123 */
 
A

Antoninus Twink

You failed to quote the original, so this answer may not be really
responsive.

The "You failed to quote the original" qualification is redundant.
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,208
Latest member
RandallLay

Latest Threads

Top