[lug] variable args

D. Stimits stimits at idcomm.com
Fri May 4 13:51:14 MDT 2001


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
> 
> _______________________________________________
> 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