Passing ... as a param?

This is a discussion on Passing ... as a param? within the C Programming forums, part of the General Programming Boards category; I'm trying to abstract out this logging API I'm using. The prototype for the logging function is Code: log(int level, ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    119

    Passing ... as a param?

    I'm trying to abstract out this logging API I'm using. The prototype for the logging function is
    Code:
    log(int level, char * fmt, ...)
    So I wanted to make my own function
    Code:
    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
    Location
    Black River
    Posts
    128
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can't you use C99 variadic argument macro?

    Code:
    #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, 11:26 AM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12: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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21