Page 1 of 1

Enumerated types and arrays

PostPosted: 09 Jul 2005, 09:29
by Tord Romstad
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

Re: Enumerated types and arrays

PostPosted: 09 Jul 2005, 10:41
by milix
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.

Re: Enumerated types and arrays

PostPosted: 09 Jul 2005, 10:58
by Tord Romstad
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

Re: Enumerated types and arrays

PostPosted: 09 Jul 2005, 11:08
by Alessandro Scotti
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.

Re: Enumerated types and arrays

PostPosted: 09 Jul 2005, 11:24
by Tord Romstad
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

Re: Enumerated types and arrays

PostPosted: 09 Jul 2005, 13:17
by Sven Schüle
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