[lug] variable length parameters

Holshouser, David dholshou at ball.com
Thu May 3 15:10:12 MDT 2001


wonderful!!!
now I have two ways to do it.
You guys rock!!

the following test produces:

foo1bar
foo1bar

============================
#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)
{
    int i=1;

    test("foo%dbar\n", i);
}
===============================

> -----Original Message-----
> From: Scott A. Herod [mailto:herod at interact-tv.com]
> Sent: Thursday, May 03, 2001 2:57 PM
> To: lug at lug.boulder.co.us
> Subject: Re: [lug] variable length parameters
> 
> 
> Try instead:
> 
> ======================================
> #include<stdio.h>
> #include<stdarg.h>
> 
> void test(char* fmt, ...)
> {
>     va_list ap;
> 
>     va_start (ap, fmt );
> 
>     printf( fmt, va_arg( ap, va_list ) );
> 
>     va_end( ap );
> }
> 
> int main (void)
> {
>     int i=1;
> 
>     test( "foo%dbar\n", i );
> }
> ======================================
> 
> Scott
> 
> "Holshouser, David" wrote:
> > 
> > This is a pure c programming question, if there is a better 
> place to ask it,
> > I don't know about it.
> > 
> > I'm trying to build a few functions for error and status logging.
> > What I want to do is take a variable length parameter list, 
> act according to
> > which function was called, and then pass the stuff on to 
> printf or maybe my
> > own bastardised printf.
> > 
> > Question:
> > can I have a function that takes variable parameters and pass those
> > parameters on [with/out modification] to another function that takes
> > variable parameters.
> > 
> > my simple test:
> > =========================================================
> > #include<stdio.h>
> > #include<stdarg.h>
> > 
> > void test(char *fmt, ...)
> > {
> >     va_list ap;
> >     va_start(ap, fmt);
> >     printf(fmt, ap);
> >     va_end(ap);
> > }
> > 
> > int main (void)
> > {
> >     int i=1;
> > 
> >     test("foo%dbar\n", i);
> > }
> > =========================================================
> > 
> > my simple test bombs with:
> > foo37813904bar
> > as output.
> > 
> > Any ideas on how to accomplish this?
> > 
> > --
> > David Holshouser
> > Engineer I
> > Ball Aerospace & Technologies Corp.
> > (303)939-5085  dholshou at ball.com
> > 
> > _______________________________________________
> > Web Page:  http://lug.boulder.co.us
> > Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> 



More information about the LUG mailing list