Mapping a macro with variable number of arguments to a variadic function

Discussion in 'C Programming' started by rashmi, Dec 5, 2006.

  1. rashmi

    rashmi Guest

    Hello All,
    Can we map a MACRO with variable number of arguments to a function with
    variable number of arguments?
    Please help me in finding out how this could be done ?
    for eg:
    #define MY_MACRO(int mid,int mlevel,...)
    my_func(mid,mlevel,format,##args)
    void my_func(int mid,int mlevel,char *format,....)
    {
    va_list ap;
    va_start(ap,format);
    vprintf(format,ap);
    va_end(ap);
    }

    int main()
    {
    int a=10;
    MY_MACRO(1,1,"hello world %d\n",a);
    return 0;
    }
    when i compile this code I get this error:
    "test2.c:23: warning: passing arg 3 of `my_func' makes pointer from
    integer without a cast"

    Thanks in advance ,
    Rashmi
    rashmi, Dec 5, 2006
    #1
    1. Advertising

  2. rashmi

    Dead Loop Guest

    rashmi wrote:
    > Hello All,
    > Can we map a MACRO with variable number of arguments to a function with
    > variable number of arguments?
    > Please help me in finding out how this could be done ?
    > for eg:
    > #define MY_MACRO(int mid,int mlevel,...)
    > my_func(mid,mlevel,format,##args)
    > void my_func(int mid,int mlevel,char *format,....)
    > {
    > va_list ap;
    > va_start(ap,format);
    > vprintf(format,ap);
    > va_end(ap);
    > }
    >
    > int main()
    > {
    > int a=10;
    > MY_MACRO(1,1,"hello world %d\n",a);
    > return 0;
    > }


    I don't think we can use macro in this way and
    I usually define several macros
    e.g. TRACE_EVENT(e), TRACE_EVENT_1P(e, p1),
    TRACE_EVENT_2P(e, p1, p2)...
    to deal with this situation. :)
    Dead Loop, Dec 5, 2006
    #2
    1. Advertising

  3. rashmi

    Tom St Denis Guest

    rashmi wrote:
    > Hello All,
    > Can we map a MACRO with variable number of arguments to a function with
    > variable number of arguments?
    > Please help me in finding out how this could be done ?


    #define printk(...) fprintf(stderr, __VA_ARGS__);

    With GCC (I dunno if that's actually part of the C spec).

    Tom
    Tom St Denis, Dec 5, 2006
    #3
  4. rashmi

    matevzb Guest

    Dead Loop wrote:
    <snip>
    > I don't think we can use macro in this way and
    > I usually define several macros
    > e.g. TRACE_EVENT(e), TRACE_EVENT_1P(e, p1),
    > TRACE_EVENT_2P(e, p1, p2)...
    > to deal with this situation. :)


    C99, 6.9.3 Macro replacement
    <snip>
    12 If there is a ... in the identifier-list in the macro definition,
    then the trailing arguments, including any separating comma
    preprocessing tokens, are merged to form a single item: the variable
    arguments. The number of arguments so combined is such that, following
    merger, the number of arguments is one more than the number of
    parameters in the macro definition excluding the ...)

    Plus, this has been available as a gcc extension for years

    --
    WYCIWYG - what you C is what you get
    matevzb, Dec 5, 2006
    #4
  5. rashmi

    rashmi Guest

    Re: Mapping a macro with variable number of arguments to a varia

    but I see only those examples the macros which are being mapped to standard
    functions like printf or fprintf can it be mapped to user defined variadic
    functions also.
    rashmi, Dec 6, 2006
    #5
  6. rashmi

    rashmi Guest

    Re: Mapping a macro with variable number of arguments to a varia

    but I see only those examples the macros which are being mapped to standard
    functions like printf or fprintf can it be mapped to user defined variadic
    functions also.
    rashmi, Dec 6, 2006
    #6
  7. rashmi

    rashmi Guest

    Re: Mapping a macro with variable number of arguments to a varia

    Thanks for your reply!!
    it is possible to map variadic macro to a variadic function
    #define TEST_LOG2(m,a,msg,...) test_fn(m,a,msg,__VA_ARGS__)
    int test_fn(int m,int i,char *msg,...);
    rashmi, Dec 7, 2006
    #7
    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. Colin Walters
    Replies:
    2
    Views:
    509
    Ben Pfaff
    Feb 13, 2004
  2. Ross A. Finlayson
    Replies:
    19
    Views:
    582
    Keith Thompson
    Mar 10, 2005
  3. Replies:
    2
    Views:
    334
    Dave Thompson
    Feb 27, 2006
  4. Replies:
    5
    Views:
    354
  5. Replies:
    3
    Views:
    805
Loading...

Share This Page