void myPrintf(const char* format, ...) {
// some code
va_list vl;
printf(format, vl);
}
int main() {
myPrintf("%d\n", 78);
}
In this code I have tried to pass the argument from ellipsis to printf. It compiles but prints garbage instead of 78. What is the right way of doing it?
You need to do the following:
void myPrintf(const char *format, ...) {
va_list vl;
va_start(vl, format);
vprintf(format, vl);
va_end(vl);
}
Please note the use of vprintf instead of printf.
First Initialize vl
va_start(vl,1); //No. of arguments =1
Then take the int from it
printf(format, va_arg(vl,int));
来源:https://stackoverflow.com/questions/18715895/how-to-pass-argument-got-from-ellipsis-operator-to-other-function