Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 09:35 PM
C_guy
Guest
 
Posts: n/a
Default typedef question

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!
  #2  
Old September 4th, 2008, 10:05 PM
C_guy
Guest
 
Posts: n/a
Default Re: typedef question

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!
  #3  
Old September 4th, 2008, 10:05 PM
C_guy
Guest
 
Posts: n/a
Default Re: typedef question

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!
  #4  
Old September 4th, 2008, 10:25 PM
Bartc
Guest
 
Posts: n/a
Default Re: typedef question


"C_guy" <erniedude@gmail.comwrote in message
news:f578edd3-c339-4cec-944e-baa8308a28b6@26g2000hsk.googlegroups.com...
Quote:
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.?

--
Bartc

  #5  
Old September 4th, 2008, 10:25 PM
Malcolm McLean
Guest
 
Posts: n/a
Default Re: typedef question


"C_guy" <erniedude@gmail.comwrote in message news:
Quote:
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...
>
just do lots of
#define FRED 1
#define JIM 2

and so on. You lose a bit of safety, but that's largely illusory anyway. You
can easily write

short person = FRED;


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

  #6  
Old September 4th, 2008, 10:35 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: typedef question

C_guy wrote:
Quote:
Thanks for the reply Rick.
(Wasn't his name Jensen Somers?)
Quote:
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...
"What Jensen Somers said." In other words: No, not unless the
compiler has special options/pragmas/whatever to control it.

However, it may not be all that big a deal. Remember that C's
enumerated types are really just integers with funny names and with
compiler-selected underlying types. Remember, too, that the named
values are just `int' constants, like `1' or `42' spelled strangely.
Since they are `int' constants, you can use them with any integer
type that can accommodate their values:

enum Field { WINKEN, BLINKEN, NOD };
char c = WINKEN;
short s = BLINKEN;
int i = NOD;

Also, since the enumerated type itself is simply an integer (of
some kind), variables of that type can be assigned values that are
not in the enumerated list:

enum Field f = BLINKEN + 41;
enum Field g = NOD * 3.14159; /* same as ... g = 6; */

The upshot is that if you really want a two-byte enumeration, you
give up almost nothing by choosing a plain two-byte integer and
using it to store the enumerated values. Thus, the problem reduces
to finding a two-byte integer -- still not trivial, but more easily
approached than trying to control the "width" of the enumerated
type itself.

The "almost nothing" is not exactly "nothing;" you do lose a
little bit by using a plain integer. If you `switch' on an enumerated
type and the cases do not cover all the enumerated values, as in

enum Field h = ...;
switch (h) {
case WINKEN: ...; break;
case NOD: ...; break;
}

.... some compilers will warn you that a case appears to be missing,
and the warning may help you detect an oversight in your code. But
if you `switch' on an ordinary integer that happens to hold an
enumerated value

int i = BLINKEN;
switch (i) {
case WINKEN: ...; break;
case NOD: ...; break;
}

.... the compiler will probably not issue such a warning. You could
write `switch ( (enum Field) i )' in hopes of re-enabling the warning,
but that's a difficult discipline to enforce in a big project.

Face it: C's enumerated types are not as "strong" as one might
wish them to be. Too bad, but that's the way it is.

--
Eric.Sosman@sun.com
  #7  
Old September 4th, 2008, 11:25 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: typedef question

C_guy <erniedude@gmail.comwrites:
Quote:
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.)
Quote:
{
>
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 };

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  #8  
Old September 4th, 2008, 11:45 PM
CBFalconer
Guest
 
Posts: n/a
Default Re: typedef question

C_guy wrote:
Quote:
>
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 */

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
  #9  
Old September 5th, 2008, 02:55 AM
Jack Klein
Guest
 
Posts: n/a
Default Re: typedef question

On Thu, 04 Sep 2008 22:42:53 +0200, Jensen Somers
<jensen@sig.see.invalidwrote in comp.lang.c:
Quote:
C_guy wrote:
Quote:
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!
>
As far as I know the standard does not specify the size of an enum. The
compiler is allowed to use any type which can contain all of the defined
values. (If I'm wrong, please correct me.)
>
I found the following reference which should apply to the C99 standard:
Each *enumerated type* shall be compatible with an integer type. The
choice of type is implementation-defined, but shall be capable of
representing the values of all the members of the enumeration
>
Most compilers will use a default value of 4 bytes but allow you to
change this via a compilation flag. Consult your compilers documentation
for more information about this.
You were doing just fine until you got to the last paragraph, and then
you blew it.

First, what do you consider "most compilers" to include?

Second, I do know of a fair number of compilers (probably more than
your "most compilers") that will use a signed int for an enumerated
type, but what does that have to do with four bytes? There are far
more compilers where type int has two bytes than four. And some where
int has one byte.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  #10  
Old September 5th, 2008, 06:45 PM
Antoninus Twink
Guest
 
Posts: n/a
Default Re: typedef question

On 4 Sep 2008 at 22:38, CBFalconer wrote:
Quote:
You failed to quote the original, so this answer may not be really
responsive.
The "You failed to quote the original" qualification is redundant.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles