[lug] variable args
Ken Weinert
kenw at ihs.com
Fri May 4 15:13:19 MDT 2001
#include<stdio.h>
#include<stdarg.h>
#define logMsg printf
char* test1(char *fmt, ...)
{
static char str[256]; /* some insanely large number? */
va_list ap;
va_start(ap, fmt);
vsprintf(str, fmt, ap);
// fprintf(stderr, str, va_arg( ap, va_list ) );
va_end(ap);
return str;
}
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);
logMsg("%s", test1("%d %d %d\n", 4, 5, 6));
}
----------------
output
123
456
* Holshouser, David (dholshou at ball.com) [010504 20:10]:
> I cannot use [vf]printf for 2 reasons.
> I am working in a restricted environment (hardware&software. VxWorks rtos)
> and must (by mandate from above) use a function called logMsg to do any
> printing.
>
> notice the 126 in the first output line.
> if I change the magic number in the call to strncpy, that also changes.
>
> output:
> -------
> 1 126 44367872
> 2 44367872 431
>
>
>
> void test(char *fmt, ...)
> {
> char str[128];
> va_list ap;
>
> str[0]='\0';
> va_start(ap, fmt);
>
> strncpy(str, fmt, 126);
> printf(str, va_arg( ap, va_list ) );
> fprintf(stderr, str, va_arg( ap, va_list ) );
>
> va_end(ap);
> }
>
>
>
>
>
> > -----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
> > >
> > > _______________________________________________
> > > 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
> >
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
--
Ken Weinert kenw at ihs.com 303-858-6956 (V) 303-705-4258 (F)
GnuPG KeyID: 9274F1CE GnuPG available at http://www.gnupg.org/
GnuPG Key Fingerprint: 1D87 3720 BB77 4489 A928 79D6 F8EC DD76 9274 F1CE
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20010504/872c6f92/attachment.pgp>
More information about the LUG
mailing list