Unix Programming - Re: Q: C macro's for lvalue statements ? Any C marco Guru's out

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > June 2004 > Re: Q: C macro's for lvalue statements ? Any C marco Guru's out





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Q: C macro's for lvalue statements ? Any C marco Guru's out
Måns Rullgård

2004-06-28, 8:59 am

"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 could
> 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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com