By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,397 Members | 2,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,397 IT Pros & Developers. It's quick & easy.

va_list passthrough

P: n/a
Is there any way to pass a variable list to a function, so as to act
as a passthrough?

for example:

void new_printf ( char *buf , ... )
{
va_list vlist;

<modify buf slightly>

printf( buf , vlist );
}
Jul 7 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
raphfrk wrote:
Is there any way to pass a variable list to a function, so as to act
as a passthrough?

for example:

void new_printf ( char *buf , ... )
{
va_list vlist;

<modify buf slightly>

printf( buf , vlist );
}
Use va_copy.

Jul 7 '08 #2

P: n/a
On Jul 7, 11:03 am, santosh <santosh....@gmail.comwrote:
raphfrk wrote:
Is there any way to pass a variable list to a function, so as to act
as a passthrough?
for example:
void new_printf ( char *buf , ... )
{
va_list vlist;
<modify buf slightly>
printf( buf , vlist );
}

Use va_copy.

I don't think that is what I want.

I want a program that would allow something like the following to
work.

#include <stdio.h>

int main()
{

int a=7l;

new_printf("%d\n" , a );

return 0;
}

void new_printf ( char *buf , ... )
{

printf( buf , ... ); // the va list is passed to printf

}

The effect of the above would be that new_printf acts the same as
printf.
Jul 7 '08 #3

P: n/a
On Jul 7, 3:01*pm, raphfrk <raph...@netscape.netwrote:
Is there any way to pass a variable list to a function, so as to act
as a passthrough?

for example:

void new_printf ( char *buf , ... )
{
* va_list vlist;

* <modify buf slightly>

* printf( buf , vlist );

}

Use va_copy.
#include <stdarg.h>
void va_copy(va_list dest, va_list src);

The prototype is self explanatory. How do you plan to modify va_list?
Jul 7 '08 #4

P: n/a
On Jul 7, 11:59 am, rahul <rahulsin...@gmail.comwrote:
On Jul 7, 3:01 pm, raphfrk <raph...@netscape.netwrote:
Is there any way to pass a variable list to a function, so as to act
as a passthrough?
for example:
void new_printf ( char *buf , ... )
{
va_list vlist;
<modify buf slightly>
printf( buf , vlist );
}

Use va_copy.
#include <stdarg.h>
void va_copy(va_list dest, va_list src);

The prototype is self explanatory. How do you plan to modify va_list?

I don't, I want to pass the va_list to the printf function (any
modifications would be to the buf field).
Jul 7 '08 #5

P: n/a
In article <62**********************************@j22g2000hsf. googlegroups.com>,
raphfrk <ra*****@netscape.netwrote:
....
>Use va_copy.
#include <stdarg.h>
void va_copy(va_list dest, va_list src);

The prototype is self explanatory. How do you plan to modify va_list?


I don't, I want to pass the va_list to the printf function (any
modifications would be to the buf field).
For some reason, nobody here wants to tell you about the vprintf() function.

This is in line with official, stated, CLC policy - which is to never
give a sucker an even break.

Jul 7 '08 #6

P: n/a
raphfrk <ra*****@netscape.netwrites:
On Jul 7, 11:03 am, santosh <santosh....@gmail.comwrote:
>raphfrk wrote:
Is there any way to pass a variable list to a function, so as to act
as a passthrough?
Only if it accepts a va_list.
for example:
void new_printf ( char *buf , ... )
{
va_list vlist;
<modify buf slightly>
printf( buf , vlist );
}

Use va_copy.


I don't think that is what I want.

I want a program that would allow something like the following to
work.

#include <stdio.h>

int main()
{

int a=7l;

new_printf("%d\n" , a );

return 0;
}

void new_printf ( char *buf , ... )
{

printf( buf , ... ); // the va list is passed to printf

}

The effect of the above would be that new_printf acts the same as
printf.
Thare are v* versions of the printf family of function for this
purpose. You want vprintf.

--
Ben.
Jul 7 '08 #7

P: n/a
On Jul 7, 12:21 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <623e246f-f0ff-4bbf-a334-a9bb5aecf...@j22g2000hsf.googlegroups.com>,raphfrk <raph...@netscape.netwrote:

...
Use va_copy.
#include <stdarg.h>
void va_copy(va_list dest, va_list src);
The prototype is self explanatory. How do you plan to modify va_list?
I don't, I want to pass the va_list to the printf function (any
modifications would be to the buf field).

For some reason, nobody here wants to tell you about the vprintf() function.
Thanks. I guess I can assume from the existance of vprintf, that
there is no way to pass a va_list directly to printf itself ?
This is in line with official, stated, CLC policy - which is to never
give a sucker an even break.
Generally, they will correct every error in your code, including those
unrelated to the original question. However, they usually do also
answer the question too (and the list of errors can be helpful too).
Jul 7 '08 #8

P: n/a
raphfrk wrote:
Thanks. I guess I can assume from the existance of vprintf, that
there is no way to pass a va_list directly to printf itself ?
That's not so bad if you realize if you realize that
printf may use, and frequently does use, vfprintf,
either directly or through vprintf.

int printf(const char *format, ...)
{
int count;
va_list arg;

va_start(arg, format);
count = vprintf(format, arg);
va_end(arg);
return count;
}

int vprintf(const char *format, va_list arg)
{
return vfprintf(stdout, format, arg);
}

I have a toy library.
http://www.mindspring.com/~pfilandr/C/library/std_io.c

--
pete
Jul 7 '08 #9

P: n/a
In article <c7**********************************@m44g2000hsc. googlegroups.com>,
raphfrk <ra*****@netscape.netwrote:
>On Jul 7, 12:21 pm, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
>In article
<623e246f-f0ff-4bbf-a334-a9bb5aecf...@j22g2000hsf.googlegroups.com>,raphfrk
<raph...@netscape.netwrote:
>>
...
>Use va_copy.
#include <stdarg.h>
void va_copy(va_list dest, va_list src);
>The prototype is self explanatory. How do you plan to modify va_list?
>I don't, I want to pass the va_list to the printf function (any
modifications would be to the buf field).

For some reason, nobody here wants to tell you about the vprintf() function.

Thanks. I guess I can assume from the existance of vprintf, that
there is no way to pass a va_list directly to printf itself ?
Correct. What this means is that every variable-number-of-args function
that exists must have a corresponding v...() version. Luckily, there
aren't very many (off the top of my head, I can only think of the printf
and scanf families) that "come with the system". But it also means that
any that you write yourself, you should also provide a v...() version of.

Which, IMHO, makes the whole va_args functionality rather useless. I
think it mainly exists for backwards compatibility with the existing
printf and scanf families.
>This is in line with official, stated, CLC policy - which is to never
give a sucker an even break.

Generally, they will correct every error in your code, including those
unrelated to the original question.
Human compilers, as they say. And about as useful. If I want a
compiler, I know where to find them.
>However, they usually do also answer the question too (and the list of
errors can be helpful too).
My experience has been otherwise. Note that I am not talking about my
own issues - i.e., I'm not whining about them not answering my questions;
I have long since realized that looking for help here is like looking in
the sewer. I'm talking about my observations of years and years of them
joyously smackin' the newbies given any opportunity.

You may, of course, have another opinion. Fantasies die hard.

Jul 7 '08 #10

P: n/a
In article <c7**********************************@m44g2000hsc. googlegroups.com>
raphfrk <ra*****@netscape.netwrote:
>... I guess I can assume from the existance of vprintf, that
there is no way to pass a va_list directly to printf itself ?
The conclusion here is correct -- you must call vprintf() rather
than printf() -- but the premise itself is faulty. In the past,
some implementations of printf() had a "%r" format that meant (more
or less) "the corresponding argument is a va_list that you should
switch to for all subsequent arguments". (In fact, at least one
%r directive took a format followed by a va_list, and worked them
in a sort of recursive fashion, hence the 'r' could be said to
stand for "recursive". Since %r was never standardized, other
printf()s that handled %r may have done other things.)

(To prove the need for the vprintf() family, you can simply read
the definition for printf() in the appropriate C Standard, and see
that there is no standard directive that takes a va_list.)

As a general rule, if you ever write your own variadic function:

TYPE my_variadic_function(T1 fixed1, T2 fixed2, ...) {
... some code will go here ...
}

you should begin by writing its entire guts as a "v"-prefixed
variant:

TYPE vmy_variadic_function(T1 fixed1, T2 fixed2, va_list ap) {
... the guts of the function ...
return whatever;
}

and then write the "wrapper" as the relatively short:

TYPE my_variadic_function(T1 fixed1, T2 fixed2, ...) {
TYPE ret;
va_list ap;

va_start(ap, fixed2);
ret = vmy_variadic_function(fixed1, fixed2, ap);
va_end(ap);
return ret;
}

That way, even though you (presumably) do not need it yourself yet,
the "v"-prefixed variant exists for whoever -- possibly you --
discovers a need for it later, in the same way that vprintf()
existed for you when you recently discovered a need for it.

The original C89 standard failed to provide a vscanf() family.
This is fixed in C99.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jul 13 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.