How to use __VA_ARGS__ inside a C function instead of macro? -


possible duplicate:
c/c++: passing variable number of arguments around

i'm using following macro declared on c file.

#define common_print(...) printf (__va_args__) 

now call works fine, turns out need able create c function looks this:

void common_print(...) {         printf (__va_args__); } 

so function doesn't work, error

"error : undefined identifier __va_args__"

the complexity of project requires have function since it's interface... how can parameters ... , pass them printf function? or better doing wrong?

thanks!

each of ?printf functions has corresponding v?printf function same thing takes va_list, variable argument list.

#include <stdio.h> #include <stdarg.h>  void common_print(char *format, ...) {     va_list args;      va_start(args, format);     vprintf(format, args);     va_end(args); } 

side note: notice va_start takes name of last argument before ... parameter. va_start macro stack-based magic retrieve variable arguments , needs address of last fixed argument this.

as consequence of this, there has @ least 1 fixed argument can pass va_start. why added format argument instead of sticking simpler common_print(...) declaration.

see: http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -