Thread: Passing ... as a param?

  1. #1
    Registered User
    Join Date
    Nov 2008

    Passing ... as a param?

    I'm trying to abstract out this logging API I'm using. The prototype for the logging function is
    log(int level, char * fmt, ...)
    So I wanted to make my own function
    mylog(int level, char * fmt, ...)
    That calls theirs. Is there some way I can pass the ... arg from my function into the actual logging function?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Black River
    You can't do that. At most, you can create a va_list out of the ellipsis and pass it to some function similar to `log' that takes a va_list in place of variable arguments. Or if your compiler supports variadic macros, then you can use one instead of a function call.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    All the standard variadic functions eg.
    printf( const char *fmt, ... );
    also have an equivalent varargs function
    vprintf( const char *fmt, va_list ap );

    Writing myprintf() would use the va_start/va_end macros, and call the vprintf() function at some point.

    Unless you have a vlog(int level, va_list ap), you're pretty much stuck without resorting to extreme trickery.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2010
    Can't you use C99 variadic argument macro?

    #define mylog(a,...)      do { mylogfunc(a,__VA_ARGS__); \
                                           log(a,__VA_ARGS__);  } while(0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Arguments
    By bolivartech in forum C Programming
    Replies: 13
    Last Post: 10-15-2009, 01:31 PM
  2. passing an array created with malloc as a param
    By mariano_donati in forum C Programming
    Replies: 12
    Last Post: 02-20-2008, 12:26 PM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  5. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM