Macro question

Discussion in 'C Programming' started by John Blevin, Oct 16, 2003.

  1. John Blevin

    John Blevin Guest

    I can play games with macro names, to do something like:

    #define MACRO_TYPE_FOO(arg) { some_function(arg); }
    #define MACRO_TYPE(arg1, arg2) MACRO_TYPE_##arg1(arg2)

    MACRO_TYPE(FOO, 5);

    But if I try to define a macro to represent the arguments, it doesn't
    work:

    #define my_args FOO, 5

    MACRO_TYPE(my_args);

    In this case it first evaluates the MACRO_TYPE and forms
    a MACRO_TYPEmy_args (which of course is not defined), it does this
    before expanding the my_args. Do you know if there is a way
    to get it to expand the my_args before the MACRO_TYPE?

    Thanks!

    - John
    John Blevin, Oct 16, 2003
    #1
    1. Advertising

  2. On Thu, 16 Oct 2003 11:23:21 -0400, John Blevin <>
    wrote:

    > I can play games with macro names, to do something like:
    >
    > #define MACRO_TYPE_FOO(arg) { some_function(arg); }
    > #define MACRO_TYPE(arg1, arg2) MACRO_TYPE_##arg1(arg2)
    >
    > MACRO_TYPE(FOO, 5);
    >
    > But if I try to define a macro to represent the arguments, it doesn't
    > work:
    >
    > #define my_args FOO, 5
    >
    > MACRO_TYPE(my_args);
    >
    > In this case it first evaluates the MACRO_TYPE and forms
    > a MACRO_TYPEmy_args (which of course is not defined), it does this
    > before expanding the my_args. Do you know if there is a way
    > to get it to expand the my_args before the MACRO_TYPE?
    >

    #define INVOKE_MACRO_TYPE(args) MACRO_TYPE(args)
    INVOKE_MACRO_TYPE(my_args);

    at least in gcc-2.95; I'm *pretty* sure it's standard, but this is one
    of the more arcane areas of the standard and ICBW.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Oct 20, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,092
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    564
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    486
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    458
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    439
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page