question about an array of enum

Henry Spencer henry at zoo.toronto.edu
Sun Nov 4 09:13:50 AEST 1990


In article <it1.657622430 at ra> it1 at ra.MsState.Edu (Tim Tsai) writes:
>typedef enum {false, true} boolean;
>	boolean bit_fields[1000];
>
>  How much memory does bit_fields actually take up, assuming a 32-bit
>architecture?  Will my C compiler treat it as an array of int's?

Almost certainly it will treat it as an array of some integer type.
(ANSI C requires that, in fact.)  The compiler is within its rights
to treat it as an array of `int', although it could also be nice and
treat it as an array of `char' or some other small integer type.

It is unlikely, verging on impossible, that you will get the effect
you want this way.  C enums are not Pascal enumerations, and the
compiler would have to work fairly hard to be sure that `true' and
`false' were the only values you were assigning to elements of that
array.  There would also be problems with things like `sizeof'.
Finally, indexing into arrays is a colossal pain if the array elements
are smaller than the minimum addressable unit, which is typically the
`char', and compilers seldom want to take the trouble, especially since
it means bending the rules anyway.

If you really want the effect of an array of bits, you're going to have
to do the two-part addressing (get a byte/word and then pick out the bit
with a mask or a shift) yourself.
-- 
"I don't *want* to be normal!"         | Henry Spencer at U of Toronto Zoology
"Not to worry."                        |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list