Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Static array with #defined # of "extra" members |
| Date | 2012-08-03 22:43 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <jvi27t$log$1@dont-email.me> (permalink) |
| References | <Yr%Sr.76276$FV6.41256@newsfe05.iad> |
On 8/3/2012 10:05 PM, Ian Pilcher wrote:
> I need to create a static array of structures with the following
> characteristics:
>
> * The first few members of the array are pre-initialized
> * The remaining members of the array are initialized to "zero"
> * The number of additional members is determined by a #defined constant,
> independently of the number of pre-initialized members
>
> Here's what I've come up with:
>
>
> #define NUM_EXTRA_FOOS 5
>
> struct foo {
> int data;
> };
>
> static struct {
> struct foo builtin[5];
> struct foo extra[NUM_EXTRA_FOOS];
> } all_foos = {
> { { 1 }, { 2 }, { 3 }, { 4 }, { 5 } },
> { { 0 } }
> };
>
> #define foos all_foos.builtin
>
> I believe that I can effectively use foos[] as an array with 10 (5 +
> NUM_EXTRA_FOOS) elements.
>
> * Is my belief correct?
The struct all_foos will be at least large enough for 10
elements, possibly larger if there's (perverse) padding.
However, trying to use all_foos.builtin[5] might not work
(see "struct hack") and even if it works it is not necessarily
the case that &all_foos.builtin[4]+1 == &all_foos.extra[0]. You
need to use something like
#define foos ((struct foo*)&all_foos)
> * Is there any danger of the compiler inserting unwanted padding between
> the builtin and extra members of the anonymous struct?
In principle, yes. Padding can appear after any struct element,
even if the implementor's reasons for putting it there don't seem
convincing.
> * Is there a better way to do this?
struct foo foos[5 + NUM_EXTRA_FOOS] =
{ {1}, {2}, {3}, {4}, {5} };
The compiler might warn about the incomplete initializer, but C
defines the effect: The extras are initialized to zero-value. You
might be able to suppress the warning with a C99-style initializer:
struct foo foos[5 + NUM_EXTRA_FOOS] =
{ {1}, {2}, {3}, {4}, {5}, [5+NUM_EXTRA_FOOS-1] = {0} );
... or even
struct foo foos[] =
{ {1}, {2}, {3}, {4}, {5}, [5+NUM_EXTRA_FOOS-1] = {0} );
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Static array with #defined # of "extra" members Ian Pilcher <arequipeno@gmail.com> - 2012-08-03 21:05 -0500
Re: Static array with #defined # of "extra" members "China Blue [Tor], Meersburg" <chine.bleu@yahoo.com> - 2012-08-03 19:40 -0700
Re: Static array with #defined # of "extra" members Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-03 22:43 -0400
Re: Static array with #defined # of "extra" members Ian Pilcher <arequipeno@gmail.com> - 2012-08-04 10:00 -0500
Re: Static array with #defined # of "extra" members Philip Lantz <prl@canterey.us> - 2012-08-03 16:46 -1000
Re: Static array with #defined # of "extra" members Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-04 16:19 +0200
csiph-web