Re: Initialization of an array and symbolic names for index
- From: pozz <pNoOzSzPuAgMno@xxxxxxxxx>
- Date: Sun, 29 Apr 2007 15:08:23 GMT
Barry Schwarz ha scritto:
This system works well but there is a drawback. When I insert a new record inHow can this work? Your header file declares mys to be a single
struct. Your code in third.c accesses mys as an array. You should be
getting a syntax error.
You are right. In source.h I should declare:
--- source.h ---
....
extern mystruct mys[];
....
---
the middle of the array I should remember to change and re-align the enumeration in the source.h file. Is there a way, maybe using some preprocessor directives, so that I modify/reorder/insert the records only in a point and maintain the coherence in the enumeration?
After you fix the above problem, the answer is still no.
Adding a new element to the array is done by updating source.c and
then compiling it. At this point in time, source.h must exist in
order to be included. Anything you do at this time, such as
preprocessing directives in source.c, will affect only the object file
being generated. Nothing will ever be written back to source.h.
#include is an input operation, not an update one. When you compile
third.c, source.h does not "remember" anything that happened while
compiling source.c.
Ok, I agree with you, but I needn't to have the same structure of files as above. I can use a third file (array.def) to list the names and the values and include it in source.h and source.c, of course with some preprocessing tricks.
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.
.
- Follow-Ups:
- Re: Initialization of an array and symbolic names for index
- From: Barry Schwarz
- Re: Initialization of an array and symbolic names for index
- 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
- Initialization of an array and symbolic names for index
- Prev by Date: Re: Common Routine To Call (Almost) Identical Functions
- Next by Date: Re: break statement in a for loop
- Previous by thread: Re: Initialization of an array and symbolic names for index
- Next by thread: Re: Initialization of an array and symbolic names for index
- Index(es):
Relevant Pages
|
|