Enumerated types and arrays

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

Enumerated types and arrays

Postby Tord Romstad » 09 Jul 2005, 09:29

Hi all,

I have a very basic C programming question. Assume that I have the following definition in my code:

Code: Select all
typedef enum {
    FOO, BAR, BAZ, GAZONK
} foo_t;

At least with my C compiler (gcc 4.0 for Mac OS X) variables of type foo_t seem to be stored as 32-bit integers. This is sometimes a bit inconvenient, especially when I need a big array of an enumerated type. I want to be able to use a declaration like
Code: Select all
foo_t foo_array[FOO_ARRAY_SIZE];

and get an array of unsigned chars. Is this possible?

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Enumerated types and arrays

Postby milix » 09 Jul 2005, 10:41

Hi Tord,
I think that all enumerations stored internally as ints, unless a compiler has a specific switch to change this.
I think it is not much of a problem to use unsigned char foo_array[FOO_ARRAY_SIZE];
I found no other REALLY usefull about enums except their auto-increament feature.
Anastasios Milikas
milix
 
Posts: 54
Joined: 04 Nov 2004, 19:36
Location: Greece

Re: Enumerated types and arrays

Postby Tord Romstad » 09 Jul 2005, 10:58

milix wrote:Hi Tord,
I think that all enumerations stored internally as ints, unless a compiler has a specific switch to change this.
I think it is not much of a problem to use unsigned char foo_array[FOO_ARRAY_SIZE];
I found no other REALLY usefull about enums except their auto-increament feature.

Thanks, Anastasios!

You are right, of course -- it is no big deal. I would prefer to use foo_t rather than unsigned char in the array declaration because it often makes it more clear how the contents of the array are used, but of course I can always just use:
Code: Select all
enum {FOO, BAR, BAZ, GAZONK};
typedef unsigned char foo_t;

foo_t foo_array[FOO_ARRAY_SIZE];


Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Enumerated types and arrays

Postby Alessandro Scotti » 09 Jul 2005, 11:08

Hi Tord,
the question is not so basic! :) In C, you cannot make assumptions about the size of an enumerated type. It is even possible that two enumerations have different size!

Here's a interesting explanation.
User avatar
Alessandro Scotti
 
Posts: 306
Joined: 20 Nov 2004, 00:10
Location: Rome, Italy

Re: Enumerated types and arrays

Postby Tord Romstad » 09 Jul 2005, 11:24

Alessandro Scotti wrote:Hi Tord,
the question is not so basic! :) In C, you cannot make assumptions about the size of an enumerated type. It is even possible that two enumerations have different size!

Here's a interesting explanation.

Thanks for your reply and the link, Alessandro!

I suppose the conclusion is that I simply shouldn't enum to define types, except when size doesn't matter.

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Enumerated types and arrays

Postby Sven Schüle » 09 Jul 2005, 13:17

Hi Tord,

here's another way of doing it, using a typedef to get the correct element size and an anonymous enum for the constants:
Code: Select all
typedef unsigned char foo_t;
enum { FOO, BAR, BAZ, GAZONK };
foo_t foo_array[FOO_ARRAY_SIZE];
/* ... */
foo_t my_foo = FOO;
/* ... */
if (my_foo <= GAZONK) {
    foo_array[i] = my_foo;
}

At least C++ allows this, I'm not really sure whether this is allowed in (ANSI) C. It is not the nicest example of strong typing but I think it's absolutely perfect for defining a small set of integer constants and using them in an 8 (or 16) bit context. It is a bit more handy than using #define's. The constants have to fit into 8 (or 16) bit, of course.

Sven
User avatar
Sven Schüle
 
Posts: 240
Joined: 26 Sep 2004, 20:19
Location: Berlin, Germany


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 35 guests