Re: Initialization of an array and symbolic names for index
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sun, 29 Apr 2007 15:09:41 -0700
On Sun, 29 Apr 2007 15:08:23 GMT, pozz <pNoOzSzPuAgMno@xxxxxxxxx>
wrote:
Barry Schwarz ha scritto:
snip issues no longer in question
I was thinking about the following solution.
--- array.def ---
MACRO( RECORD1, value1 )
MACRO( RECORD2, value2 )
...
MACRO( RECORDn, valuen )
---
--- source.h ---
#undef MACRO
#define MACRO(sym,val) sym,
typedef struct {
int value;
} mystruct;
extern mystruct mys[];
enum MYSTRUCT_INDEX = {
#include "array.def"
};
---
--- source.c ---
#undef MACRO
#define MACRO(sym,val) { val },
mystruct mys[] = {
#include "array.def"
};
---
The above solution works well, but there is a little difference with the first
scenario. Here the struct mystruct is composed by only one member. My real
struct is composed by several fields.
In this case, I should define MACRO in source.c and source.h in the following way:
--- source.h ---
...
#undef MACRO
#define MACRO(sym, f1, f2, ..., fm) sym,
...
---
--- source.c ---
...
#undef MACRO
#define MACRO(sym, f1, f2, ..., fm) { f1, f2, ..., fm },
...
---
If I change the composition of the struct, I should modify the MACRO definition
in source.c and source.h, so I must continue preserving the coeherence of two or
three files.
To consolidate the point of change to a single (header) file which has
the additional advantage of insuring that all source files that
include the header will be recompiled when the header changes (if your
system keeps track), I would try something along the lines of
#define MACRO1(sym, f1, f2, ..., fm) sym
#define MACRO2(sym, f1, f2, ..., fm) {f1, f2, ..., fm}
#define MACRO(macro) \
macro(sym1, <field11>, <field21>, ..., <fieldm1>), \
macro(sym2, <field12>, <field22>, ..., <fieldm2>), \
...,
macro(symn, <field1n>, <field2n, ..., <fieldmn>)
enum MYSTRUCT_INDEX {
mystruct_dummy = -1, /* probably not necessary since enums
default to start at 0 but your initial code had initialization */
MACRO(MACRO1)
};
and in source.c
mystruct mys[] = {MACRO(MACRO2)};
I'm not sure if the string of macros above requires any kind of
"circumlocution" along the lines described in faq section 11.17.
Remove del for email
.
- References:
- Initialization of an array and symbolic names for index
- From: pozz
- Re: Initialization of an array and symbolic names for index
- From: Barry Schwarz
- Re: Initialization of an array and symbolic names for index
- From: pozz
- Initialization of an array and symbolic names for index
- Prev by Date: Re: Ahead of "main"?
- Next by Date: Re: String Comparision
- Previous by thread: Re: Initialization of an array and symbolic names for index
- Next by thread: First n twin primes
- Index(es):
Relevant Pages
|
|