开发者

C syntax to create function foo(...)

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜