[lug] variable args

Scott A. Herod herod at interact-tv.com
Fri May 4 14:57:13 MDT 2001


Sorry my way didn't work.  I'd still like to know how to do
something like is below though:

f1( type1 arg1, ... )
{
    another_type new_arg1;
    f2( new_arg1, ( rest of incoming arguments ) );
}

f1( another_type new_arg1, ... )
{
    ( do something with all of the arguments )
}

main()
{
    type1 arg1;
    f1( arg1, arg2, arg3 );
}

One comment.  Hard coded string lengths scare me
unless you're sure they won't overrun.  Another option
might be to fill up a strstream buffer.

Scott

"Holshouser, David" wrote:
> 
> I found the 'correct' way to do this.
> Without your 'vprintf' knowledge, I would never have found vsprintf.
> vsprintf does variable arguments in the style of printf but to a string like
> sprintf.
> Now I can do the following.
> Yay!
> 
> myfunc(fmt, ...){
>   logStuff();        // my logging
>   vsprintf(str, ap)  // make ready for printing
>   logMsg(str);       // follow their guidelines with replacement for printf.
> }
> 
> #include "debug_aid.h"
> 
> void ErrMsg(char* fmt, ...)
> {
>   va_list ap;
>   char str[128];
> 
>   va_start(ap, fmt);
>   vsprintf(str, fmt, ap);
>   printf(str);
>   va_end(ap);
> }  // ErrMsg()
> 
> void StatMsg(char* fmt, ...)
> {
>   va_list ap;
>   char str[128];
> 
>   va_start(ap, fmt);
>   vsprintf(str, fmt, ap);
>   printf(str);
>   va_end(ap);
> }  // StatMsg()
> 
> int main(void)
> {
>         char foo='c';
>         StatMsg("%d:%d:%1.1f:%c\n", 2, 3, 4.0, foo);
>         ErrMsg("%f\n", 5.0);
> }
> 
> > -----Original Message-----
> > From: D. Stimits [mailto:stimits at idcomm.com]
> > Sent: Friday, May 04, 2001 1:51 PM
> > To: lug at lug.boulder.co.us
> > Subject: Re: [lug] variable args
> >
> >
> > Out of curiosity, I'm wondering what would happen if you did
> > not buffer?
> > printf() buffers since it uses stdout, but if you instead try
> > fprintf()
> > and print to stderr, buffering is removed. Buffering could be doing
> > something odd that stderr would not act odd with. I am also
> > curious why
> > vprintf can't be used?
> >
> > Also, you might want to temporarily alter the "test" function
> > so that it
> > does not use the "fmt" argument directly; instead, strcpy (or strncpy)
> > fmt to a temporary buffer; I suggest this because for testing you want
> > to be guaranteed that "fmt" can't be changed to point at something
> > different as things go along, and a separate copy will guarantee the
> > variable argument setup does not change it.
> >
> > D. Stimits, stimits at idcomm.com
> >
> > "Holshouser, David" wrote:
> > >
> > > FYI
> > >
> > > Calling va_arg(ap, va_list) only seems to return the first
> > argument in the
> > > original list.
> > > And it turns out that I can't use vprint (or printf for
> > that matter).
> > > Back to the drawing board.
> > >
> > > The following code produces:
> > > 1 2 3
> > > 1 1627655920 144
> > >
> > > #include<stdio.h>
> > > #include<stdarg.h>
> > >
> > > void test(char *fmt, ...)
> > > {
> > >     va_list ap;
> > >     va_start(ap, fmt);
> > >     vprintf(fmt, ap);
> > >     printf( fmt, va_arg( ap, va_list ) );
> > >     va_end(ap);
> > > }
> > >
> > > int main (void)
> > > {
> > >     test("%d %d %d\n", 1, 2, 3);
> > > }
> > >
> > > --
> > > David Holshouser
> > > Engineer I
> > > Ball Aerospace & Technologies Corp.
> > > (303)939-5085  dholshou at ball.com



More information about the LUG mailing list