Re: Macro redefinition



On Aug 31, 11:04 am, Tor Rustad <tor_rus...@xxxxxxxxxxx> wrote:
I'm implementing a parser, where there is a lot of different data
elements defined. Now instead of hand-coding X defines, and the massive
number of lines needed for table initialization, I decided to generate
this source instead.

So, I basically used this trick:

void gen_source_function(FILE *out)
{
#define EXPAND_DEF(num, name) generate_source_1(out, num, #name)
#include "iso8583_defs"

#define EXPAND_DEF(num, name) generate_source_2(out, num, #name)
#include "iso8583_defs"

}

where the "iso8583_defs" file, did contain a lot of lines like this:

EXPAND_DEF(1, FIELD_NAME);
EXPAND_DEF(2, ANOTHER_FIELD_NAME);

My question is simply, is the above C code allowed?

It's a pretty common idiom in the code I work with. One caveat: don't
forget to #undef EXPAND_DEF before redefining it.

Regards,

-=Dave

.



Relevant Pages

  • Re: Macro redefinition
    ... Tor Rustad wrote: ... Now instead of hand-coding X defines, ... number of lines needed for table initialization, ... #define EXPAND_DEF(num, name) generate_source_1 ...
    (comp.lang.c)
  • Re: Macro redefinition
    ... Now instead of hand-coding X defines, ... number of lines needed for table initialization, ... found the code hard to maintain, so we dropped the technique and used ... language extension inside an established language. ...
    (comp.lang.c)
  • Macro redefinition
    ... I'm implementing a parser, where there is a lot of different data elements defined. ... Now instead of hand-coding X defines, and the massive number of lines needed for table initialization, I decided to generate this source instead. ... #define EXPAND_DEF(num, name) generate_source_1 ...
    (comp.lang.c)