Nested variadic functions

B

Boltar

Hi

I think I posted a question similar to this on here a while back but I
can't find the thread and I need something more general anyway.

My problem is I have 2 variadic functions , one of which needs to call
the other passing its entire argument list but the other needs to be
callable from anywhere else in the program as a variadic too, ie it
*can't* have va_list as a paramater type.

eg:

void func1(char *fmt, ...)
{
prepend something to format
func2(fmt,args);
}



void func2(char *fmt,...)
{

}


Is this possible? Nothing I try seems to work.

Thanks for any help

B2003
 
R

Richard Tobin

Boltar said:
My problem is I have 2 variadic functions , one of which needs to call
the other passing its entire argument list but the other needs to be
callable from anywhere else in the program as a variadic too, ie it
*can't* have va_list as a paramater type.

Have two versions of each function: one with a va_list argument and
a variadic one which just calls the other one. Then calls between
them can just use the va_list version.

The *printf() functions follow this pattern - fprintf(), vfprintf(),
fscanf(), vfscanf() etc.

-- Richard
 
K

Keith Willis

My problem is I have 2 variadic functions , one of which needs to call
the other passing its entire argument list but the other needs to be
callable from anywhere else in the program as a variadic too, ie it
*can't* have va_list as a paramater type.

eg:

void func1(char *fmt, ...)
{
prepend something to format
func2(fmt,args);
}



void func2(char *fmt,...)
{

}

How about if you provide variadic interfaces for your func1() and
func2(), and wrap the stuff that was in func2() into a new func3()
which can accept a va_list.

#include <stdio.h>
#include <stdarg.h>

void func1(char *fmt, ...)
{
/* prepend something to fmt, giving newfmt */
va_list args;
va_start (args, fmt);
func3 (newfmt, args);
va_end (args);
}

void func2(char *fmt, ...)
{
/* leave fmt unchanged */
va_list args;
va_start (args, fmt);
func3 (fmt, args);
va_end (args);
}

void func3(char *fmt, va_list arglist)
{
/* do stuff with fmt and arglist - probably like this: */
vfprintf(stderr, fmt, arglist);
}

I'm doing something similar to this - albeit in C++ - with a debug
logging class, where my equivalent of func3 is private, and I provide
a number of public interfaces which are variadic.
 
P

Pranav Peshwe

Hi

I think I posted a question similar to this on here a while back but I
can't find the thread and I need something more general anyway.

My problem is I have 2 variadic functions , one of which needs to call
the other passing its entire argument list but the other needs to be
callable from anywhere else in the program as a variadic too, ie it
*can't* have va_list as a paramater type.

eg:

void func1(char *fmt, ...)
{
prepend something to format
func2(fmt,args);

}

void func2(char *fmt,...)
{

}

Is this possible? Nothing I try seems to work.


Hi,
Apart from refactoring your code to work with variadics, you can
look for compiler extensions which might help. GNU c compiler for
example, has __builtin_apply_args()

Just a thought...

Best regards,
Pranav
 
C

Chris Dollin

Boltar said:
I think I posted a question similar to this on here a while back but I
can't find the thread and I need something more general anyway.

My problem is I have 2 variadic functions , one of which needs to call
the other passing its entire argument list but the other needs to be
callable from anywhere else in the program as a variadic too, ie it
*can't* have va_list as a paramater type.

Introduce a third function with a va_list arg that both variadic functions
call.
 

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,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top