Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I use the `...` C operator to get a function to print out all it's args?

Here's what I tried... but failed:

void testfunc(...){
    printf(...);
}
like image 814
Sam H Avatar asked Nov 29 '10 23:11

Sam H


1 Answers

This will create a function that is equivalent to printf. Note that you cannot blindly print out arguments, since you somehow need to know what type each argument is in advance. The format argument to printf informs it what arguments to expect and what types they will be.

#include <stdargs.h>

void testfunc(const char *format, ...)
{
    va_list ap;
    va_start(ap, format);

    vprintf(format, ap);

    va_end(ap);
}
like image 180
cdhowie Avatar answered Sep 25 '22 19:09

cdhowie