Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nested variadic functions

31 views
Skip to first unread message

Boltar

unread,
Nov 12, 2007, 5:37:46 AM11/12/07
to
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

Richard Tobin

unread,
Nov 12, 2007, 5:48:38 AM11/12/07
to
In article <1194863866....@57g2000hsv.googlegroups.com>,
Boltar <bolta...@yahoo.co.uk> wrote:

>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Keith Willis

unread,
Nov 12, 2007, 7:27:22 AM11/12/07
to
On Mon, 12 Nov 2007 02:37:46 -0800, Boltar <bolta...@yahoo.co.uk>
wrote:

>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.
--
PGP key ID 0xEB7180EC

Pranav Peshwe

unread,
Nov 12, 2007, 11:02:06 AM11/12/07
to


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


--------------------------------------------------------------------------
Software gets slower faster than hardware gets faster. - Niklaus Wirth

Chris Dollin

unread,
Nov 13, 2007, 3:50:08 AM11/13/07
to
Boltar wrote:

> 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.

--
Chris "sharing is all" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

0 new messages