Re: Q: C macro's for lvalue statements ? Any C marco Guru's out there ?
From: Roger Willcocks (roger_at_rops.org)
Date: 06/30/04
- Next message: Shaun Clowes: "Re: Detecting number of parameters of C function"
- Previous message: elia Mazzawi: "Re: sync in a C program?"
- In reply to: Måns Rullgård: "Re: Q: C macro's for lvalue statements ? Any C marco Guru's out there ?"
- Next in thread: Phlip: "Re: Q: C macro's for lvalue statements ? Any C marco Guru's out there ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Jun 2004 23:10:33 +0100
"Måns Rullgård" <mru@kth.se> wrote in message
news:yw1xfz8fu8pl.fsf@kth.se...
> "Gregor Copoix" <logical@xeption.de> writes:
>
> >> > 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;
> >
> >> You need to use the token merging operator (##) rather than
stringification
> >> operator (#), like this:
> >
> >> #define MAKE_VARNAME2(base, var,val) int var##_##base = val
> >
> > But this results still in an lvalue error as the concaternated text is a
> > string for the preprocessor.
> > int "var_module1" = 0;
> > and results in an compiler error.
> > I tried both # and ##.
>
> Then your bug is somewhere else. It should work with ##.
>
> --
> Måns Rullgård
> mru@kth.se
Most compilers have an option to run just the preprocessor - for gcc it's
the -E switch. Try looking at the generated code and you'll have more chance
of figuring out what's going on.
You'll find that using '##' as suggested doesn't work - it generates
int status_BASE_NAME = 0;
You need another level of indirection to substitute the BASE_NAME text. Try:
#define BASE_NAME module1
#define PASTE3(a,b,c) a##b##c
#define MAKE_VARNAME2(base,var,val) int PASTE3(var,_,base) = val
#define MAKE_VARNAME(var,val) MAKE_VARNAME2(BASE_NAME,var,val)
MAKE_VARNAME(status, 0);
-- Roger
- Next message: Shaun Clowes: "Re: Detecting number of parameters of C function"
- Previous message: elia Mazzawi: "Re: sync in a C program?"
- In reply to: Måns Rullgård: "Re: Q: C macro's for lvalue statements ? Any C marco Guru's out there ?"
- Next in thread: Phlip: "Re: Q: C macro's for lvalue statements ? Any C marco Guru's out there ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]