Sorry for the simple question, but how could I create the C-function with undefined number of parameters such as
int printf ( const char * format, ... ).
I would like to create fu开发者_StackOverflow中文版nction to use it as wrapper for printk:
void my_printk( const char * format, ...)
{
printk("my log:");
printk(format, ...);
printk("\n");
}
Thanks
You have to convert the args to a va_list before you can pass it to another function. Then you can pass it to the 'v' version of the function.
So you can do:
void my_printk( const char * format, ...)
{
va_list ap;
va_start(ap, format);
printk("my log:");
vprintk(format, ap);
printk("\n");
va_end(ap);
}
Most of the time, any function like this will provide a 'v' version, and yours should too:
void my_vprintk( const char * format, va_list ap)
{
printk("my log:");
vprintk(format, ap);
printk("\n");
}
void my_printk( const char * format, ...)
{
va_list ap;
va_start(ap, format);
my_vprintk(format, ap);
va_end(ap);
}
You're close. Have a look here: http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html
int f(int, ... ); int f(int, ... ) { . . . } int g() { f(1,2,3); }
精彩评论