Variable argument number in functions

A

Andrej Prsa

Hello!

If I was to write a function that takes variable argument number that
would simply call printf with those exact arguments, how do I pass them to
printf?

E.g.

int my_printf (const char *fmt, ...)
{
printf ( ??????????? );
}

Thanks,

Andrej
 
K

Kevin Easton

Andrej Prsa said:
Hello!

If I was to write a function that takes variable argument number that
would simply call printf with those exact arguments, how do I pass them to
printf?

E.g.

int my_printf (const char *fmt, ...)
{
printf ( ??????????? );
}

That's what the vprintf() function is for:

#include <stdarg.h>

int my_printf(const char *fmt, ...)
{
va_list ap;
int r;

va_start(ap, fmt);
r = vprintf(fmt, ap);
va_end(ap);

return r;
}

....and so, you should also write a vmy_printf function, so that your
function can itself be wrapped in the same manner:

int vmy_printf(const char *fmt, va_list ap)
{
/* Put whatever my_printf functionality you want here */
return vprintf(fmt, ap);
}

int my_printf(const char *fmt, ...)
{
va_list ap;
int r;

va_start(ap, fmt);
r = vmy_printf(fmt, ap);
va_end(ap);

return r;
}

- Kevin.
 
D

Dimitris Mandalidis

On Thu, 4 Sep 2003 11:17:39 +0200, Andrej Prsa wrote
int my_printf (const char *fmt, ...)
{
printf ( ??????????? );
}

Through va_start() and its relatives. There is a precise example in K&R II -
pg.155, but if you don't have K&RII the algorithm is something like that :

* va_start(ap, fmt) /* ap points to the first unnamed argument */
* use va_arg to move forward to the next argument
* use va_end when you have finished

HTH
D.
 
B

Ben Pfaff

Andrej Prsa said:
If I was to write a function that takes variable argument number that
would simply call printf with those exact arguments, how do I pass them to
printf?

If you had read the FAQ, you would already know.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top