473,498 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nesting variadic functions

Hi

Is it possible to nest variadic functions? Eg to do something like
this:

void mainfunc(char *fmt, ...)
{
va_list args;
va_start(args,fmt);

do something;

subfunc(fmt, args);
va_end(args);
}

void subfunc(char *fmt, ...)
{
va_list args;

va_start(args,fmt);
etc
va_end(args);
}
The above type of code will compile but the arguments in subfunc() are
corrupted. Is there a way to do this?

Thanks for any help

B2003

Sep 17 '07 #1
4 3609
Boltar said:
Hi

Is it possible to nest variadic functions? Eg to do something like
this:

void mainfunc(char *fmt, ...)
{
va_list args;
va_start(args,fmt);

do something;

subfunc(fmt, args);
va_end(args);
}

void subfunc(char *fmt, ...)
Oh, and you were doing so well.

void subfunc(char *fmt, va_list args)
{
va_list args;
Drop this...
>
va_start(args,fmt);
....and this...
etc
va_end(args);
....and this, and let the caller's va_end take the strain. (In the caller,
don't call va_args after calling subfunc - or, if you must, you'll first
have to call va_end and va_start again.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 17 '07 #2
On Sep 17, 1:20 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Boltar said:


Hi
Is it possible to nest variadic functions? Eg to do something like
this:
void mainfunc(char *fmt, ...)
{
va_list args;
va_start(args,fmt);
do something;
subfunc(fmt, args);
va_end(args);
}
void subfunc(char *fmt, ...)

Oh, and you were doing so well.

void subfunc(char *fmt, va_list args)
{
va_list args;

Drop this...
va_start(args,fmt);

...and this...
etc
va_end(args);

...and this, and let the caller's va_end take the strain. (In the caller,
don't call va_args after calling subfunc - or, if you must, you'll first
have to call va_end and va_start again.)
Works nicely. Thanks.

B2003

Sep 17 '07 #3
In article <11**********************@d55g2000hsg.googlegroups .com>,
Boltar <bo********@yahoo.co.ukwrote:
>Is it possible to nest variadic functions? Eg to do something like
this:
Unfortunately C doesn't have the equivalent of Lisp's "apply".

Look at the printf() family of functions. In particular observe how
for each function there is a "v" version, that takes a va_list instead
of multiple arguments. You may find it useful to do the same for all
your variadic functions - not just the internal ones - to make it
convenient for others to write wrappers around them.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 17 '07 #4
Boltar wrote:
>
Is it possible to nest variadic functions? Eg to do something like
this:

void mainfunc(char *fmt, ...) {
va_list args;

va_start(args,fmt);
do something;
subfunc(fmt, args);
va_end(args);
}

void subfunc(char *fmt, ...) {
va_list args;

va_start(args,fmt);
etc
va_end(args);
}

The above type of code will compile but the arguments in subfunc()
are corrupted. Is there a way to do this?
Define the parameter as "const char *fmt". Maybe 'do something' or
subfunct() is altering *fmt? (or fmt). It looks like O(n * n)
where n is the number of arguments.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 17 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1870
by: Loony | last post by:
Hi, I want to use variadic functions with stl containers as arguments but I am not sure if that is possible and if so how. I'm using gcc 3.3 on Suse 8.2 Hope someone can help me. Thanx...
3
3529
by: Loony | last post by:
Hi, I have a problem with my code. I am using a virtual class in which I define a variadic function. This class is inherited by another one which then implements it. The problem occurs while...
89
6382
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
5
2147
by: Christopher Benson-Manica | last post by:
Do variadic functions (such as printf()) always scan the format string for format specifiers, even when there are no additional arguments? If it is instead a QoI issue, what would be true for a...
19
4218
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
2893
by: Florian G. Pflug | last post by:
Hi I faintly remember that I once stumbled upon a way to declare variadic functions (functions that take a variable number of arguments) in plpgsql - but I just searched the docs and can't find...
9
3259
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
4
4916
by: Boltar | last post by:
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...
8
3406
by: Christof Warlich | last post by:
Hi, is there any way to access individual elements in the body of a variadic macro? I only found __VA_ARGS__, which always expands to the complete list. Thanks for any help, Christof
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.