Re: Initialization of an array and symbolic names for index



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
.



Relevant Pages

  • Re: what will be the value of #define
    ... please convince me that I'm wrong. ... #undef MACRO ... after the usual macro replacement, so by the time this is done, ...
    (comp.lang.c)
  • Re: End-of-the-week fun
    ... macro(x); ... int ) ... using namespace std; ...
    (comp.lang.cpp)
  • Re: Macro file save as, saving sheet not workbook
    ... But I've never seen a recorded macro get named Record1 -- unless I changed the ... always store the recorded macro in a different workbook. ... Dave Peterson ...
    (microsoft.public.excel.programming)
  • Re: next-record-if else STOP?
    ... City: Atlanta ... emailing, the macro stops (as you're no longer in Word's domain, but in your ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Challenging macro with default value
    ... On Wed, 31 Mar 2004, Macro Maniac wrote: ... I'll just re-write the whole thing. ... void algorithmOnStructField(MyStruct *arr, int narr, int ofs) ... algorithmOnStructField, offsetof(struct MyStruct, field1)); ...
    (comp.lang.c)