Newsgroups: comp.lang.c Path: utzoo!henry From: henry@zoo.toronto.edu (Henry Spencer) Subject: Re: question about an array of enum Message-ID: <1990Nov3.221350.19544@zoo.toronto.edu> Organization: U of Toronto Zoology References: Date: Sat, 3 Nov 90 22:13:50 GMT In article it1@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@zoo.toronto.edu utzoo!henry