f



Q: C macro's for lvalue statements ? Any C marco Guru's out there ?

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.

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 ?
// ---------------  cut
#define TEST1(x) #define VARNAME x
// ---------------  cut

Thank you for your help/ideas.

Gregor


0
logical (3)
6/28/2004 6:32:59 PM
comp.programming 11491 articles. 2 followers. Post Follow

1 Replies
467 Views

Similar Articles

[PageSpeed] 36

On Mon, 28 Jun 2004, Gregor Copoix wrote:
>
> The idea:
> I have a set of macros that create variable names depending of the content
> of some other define.
>
> #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);
>
> which should expand to
>
> int status_module1 = 0;

  [Whoever already responded, I saw Gregor's response but not the original
reply; I don't even know *who* replied.  Apologies for repeated advice.]

  Your first problem is using # instead of ##.  Your second problem is
that macro expansion is tricky (in pretty much any language); in this
case you need to insert another "level" of macro expansion in between the
place where you introduce the argument 'BASE_NAME' and the place where it
is "pasted" together with the rest of the arguments. Try this complete
program, and if it does not do what you expect, then ask in comp.lang.c,
which is a whole newsgroup specializing in C.  You might also search
Google; ISTR that the "Boost" library for C++ has a whole header devoted
to preprocessor tricks, and this might be one of them.


  #include <stdio.h>

  #define BASE_NAME module1
  #define MAKE_VARNAME3(base, var, val) int var##_##base = val
  #define MAKE_VARNAME2(base, var, val) MAKE_VARNAME3(base, var, val)
  #define MAKE_VARNAME(var, val) MAKE_VARNAME2(BASE_NAME, var, val)

  MAKE_VARNAME(status, 0);

  int main(void)
  {
      printf("%d\n", status_module1);
      return 0;
  }


> And by the way: Is there a way to #define anything within a #define ?
> // ---------------  cut
> #define TEST1(x) #define VARNAME x
> // ---------------  cut

  No.  You can sometimes get close to what you want by other means,
but not always.  This is a weakness of C's preprocessor.

-Arthur
0
ajo (1603)
6/28/2004 1:50:54 PM
Reply: