Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Jens Gustedt <jens.gustedt@loria.fr> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Static array with #defined # of "extra" members |
| Date | 2012-08-04 16:19 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <501D2F88.5070300@loria.fr> (permalink) |
| References | <Yr%Sr.76276$FV6.41256@newsfe05.iad> |
Am 04.08.2012 04:05, schrieb Ian Pilcher:
> 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
> #define NUM_EXTRA_FOOS 5
Yes this can be done even without much preprocessor magic.
Defining your initializers in a macro
#define FIRST_ELEMS { 1 }, { 2 }, { 3 }, { 4 }, { 5 }
and then replace your struct definition by just an array where the size
expression is constructed from the sizeof of a compount literal.
struct foo all_foos[NUM_EXTRA_FOOS + sizeof((struct foo[]){
FIRST_ELEMS})/sizeof(struct foo)] = {
FIRST_ELEMS,
};
Preprocessor magic can also be used to generated some fixed length list
of initializers, if you prefer that, but this is a bit more involved.
Jens
Back to comp.lang.c | Previous | Next — Previous 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