Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > va_list passthrough

Reply
Thread Tools

va_list passthrough

 
 
raphfrk
Guest
Posts: n/a
 
      07-07-2008
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 );
}
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-07-2008
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.

 
Reply With Quote
 
 
 
 
raphfrk
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 11:03 am, santosh <santosh....@gmail.com> wrote:
> 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.
 
Reply With Quote
 
rahul
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 3:01*pm, raphfrk <raph...@netscape.net> 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.
#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?
 
Reply With Quote
 
raphfrk
Guest
Posts: n/a
 
      07-07-2008
On Jul 7, 11:59 am, rahul <rahulsin...@gmail.com> wrote:
> On Jul 7, 3:01 pm, raphfrk <raph...@netscape.net> 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.
> #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).
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-07-2008
In article <623e246f-f0ff-4bbf-a334->,
raphfrk <> wrote:
....
>> 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.

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-07-2008
raphfrk <> writes:

> On Jul 7, 11:03 am, santosh <santosh....@gmail.com> wrote:
>> 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.
 
Reply With Quote
 
raphfrk
Guest
Posts: n/a
 
      07-07-2008
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.net> wrote:
>
> ...
>
> >> 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).
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-07-2008
In article <c74cd17e-7f3c-4353-bfb5->,
raphfrk <> wrote:
>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.net> wrote:
>>
>> ...
>>
>> >> 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.

 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      07-13-2008
In article <c74cd17e-7f3c-4353-bfb5->
raphfrk <> wrote:
>... 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 (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cisco 877 pptp passthrough Cen Cisco 1 08-17-2005 02:22 AM
"passthrough" modem on Cisco 2500 Kifla a.k.a. F. Stratocaster Cisco 1 04-14-2005 08:56 PM
Pix and IPSec Passthrough Ian E Cisco 1 07-15-2004 09:26 AM
Pix VPN Passthrough Mark Kingery Cisco 0 01-22-2004 05:02 PM
beeing a vpn gateway and doing VPN passthrough Christian Knoblauch Cisco 0 12-29-2003 04:46 PM



Advertisments