My last post -
http://groups.google.com/group/comp....df6ab20a6007d0
I have a little bit more challenging question this time. Suppose I
have
write_cmd(struct ast_connection *conn, char *msg, ...);
I want to call it like (forgive the syntax)
write_cmd(conn, str, p1, p2, ..., pn, k1, k2, ..., km);
The parameters p1 to pn must be passed to write_cmd every time. The
parameters k1 to km are not actually constant, but they are accessible
from conn; so, I don't really need to pass them in. It would be better
to call
write_cmd(conn, str, p1, p2, ..., pn);
However, as noted in the last post, I want to print the parameters
using vsprintf, which means they all need to be in the list. I could
follow vsprintf with sprintf to get the rest of k1 to Km, and thus
roughly double both the buffer space and processing time. But this
seems silly given that I could get those parameters into the list by
calling the first write_cmd.
I have some code which obviously doesn't work. But you can probably
see what I'm aiming for. I could probably also do it with a vsprintf
that took a list and a variable number of parameters, if there were
such a thing...
#include <stdio.h>
#include <stdarg.h>
void vvfunc(char *msg, va_list argp, va_list argp2)
{
char buf[128];
vsprintf(buf,msg,argp); //argp2 goes here
puts(buf);
}
void vfunc2(char *msg, va_list argp, ...)
{
va_list argp2;
va_start(argp2,argp);
vvfunc(msg,argp,argp2);
va_end(argp2);
}
void func(char *msg, ...)
{
va_list argp;
va_start(argp,msg);
vfunc2(msg,argp,10); //k1
va_end(argp);
}
int main(void)
{
func("hello %s %d\n","there"); //p1
return 0;
}