06-28-04 01:59 PM
"Gregor Copoix" <logical@xeption.de> writes:
> Hi everybody,
>
> I've got a problem implementing some macros that would make my code much
> more readable.
>
> The idea:
> I have a set of macros that create variable names depending of the content
> of some other define.
>
> An example (of how it should look like in the end):
>
> // --------------- cut
> #define BASE_NAME module1
>
> // the macro(s) which I need help for :-)
> #define MAKE_VARNAME2(base, var,val) int var#_#base = val
> #define MAKE_VARNAME(var, val) MAKE_VARNAME2(BASE_NAME, var, val)
>
> // the usage example
> MAKE_VARNAME(status, 0);
> // --------------- cut
>
> which should expand to
>
> int status_module1 = 0;
>
> I know, with the macro above this doesn't work so far, but is there a way
to
> generate lvalue names (like variable names) with C macros (like in the
> example above) ?
> The main problem is, that any string processing in macros generate real
> strings (with ""around) and the compiler doesn't accept them as lvalue -
> and now I am unfortunetly out of ideas.
You need to use the token merging operator (##) rather than stringification
operator (#), like this:
#define MAKE_VARNAME2(base, var,val) int var##_##base = val
> So it would be very helpful if there's any C macro guru out there who coul
d
> help m with this problem.
>
> And by the way: Is there a way to #define anything within a #define ?
No.
--
Måns Rullgård
mru@kth.se
[ Post a follow-up to this message ]
|