Forwarding Variable Argument Lists

J

joecook

I thought I could have a wrapper over a function that takes a variable
argument list, but it doesn't work. (it compiles, but does not produce
expected results) Anything pop out?

#include <iostream>
#include <cstdarg>

using namespace std;

void foo(const char* fmt, ...)
{
va_list ap;
va_start(ap,fmt);
printf(fmt,ap);
va_end(ap);

}

int main()
{
foo("This is a number: %d",44);

}

Thanks,
Joe C
 
P

Pascal J. Bourguignon

I thought I could have a wrapper over a function that takes a variable
argument list, but it doesn't work. (it compiles, but does not produce
expected results) Anything pop out?

#include <iostream>
#include <cstdarg>

using namespace std;

void foo(const char* fmt, ...)
{
va_list ap;
va_start(ap,fmt);
printf(fmt,ap);
va_end(ap);

}

Indeed this is not possible like this.

If you want to call a random function, you will have to use a library
such as ffcall, which will push the parameters on the stack for you.


But happily, if what you want is only the printf functionality, you
can call instead vprintf.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top