Article View: comp.std.c
Article #4379Re: Stdarg
From: Mike Rubenstein
Date: Mon, 20 Nov 1995 06:09
Date: Mon, 20 Nov 1995 06:09
43 lines
1666 bytes
1666 bytes
Jeff.Dunlop@dbsoft.gigo.com (Jeff Dunlop) wrote:
>Is it an allowable side effect for vprintf or any other vararg
>function to modify the value of the passed arglist? The following
>code fails under Watcom 10.0 because p_arg is modified by the first
>call to vprintf. If I reinitialize p_arg with va_end and va_start,
>the second vprintf works.
>
>#include <stdarg.h>
>#include <stdio.h>
>
>void test(char *fmt, ...)
>{
> va_list p_arg;
> va_start(p_arg, fmt);
> vprintf(fmt, p_arg);
> vprintf(fmt, p_arg);
> va_end(p_arg);
>}
Yes. From ISO 7.8:
If access to the varying arguments is desired, the called function
shall declare an object (refered to as ap in this subclause) having
type va_list. The object ap may be passed as an argument to another
function; if that function invokes the va_arg macro with parameter
ap, the value of ap in the calling function is indeterminate and
shall be passed to the va_end macro prior to any further reference to
ap.
To get it to work right, just change your code to:
va_list p_arg;
va_start(p_arg, fmt);
vprintf(fmt, p_arg);
va_end(p_arg);
va_start(p_arg, fmt);
vprintf(fmt, p_arg);
va_end(p_arg);
Michael M Rubenstein
Technical Details
Message-ID:
<48p630$avl@ixnews3.ix.netcom.com>Path:
arc1-ca.pugleaf.net!ix.netcom.com!netnewsReferences:
<32b_9511191940@gigo.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!ix.netcom.com!netnews From: miker3@ix.netcom.com (Mike Rubenstein) Newsgroups: comp.std.c Subject: Re: Stdarg Date: Mon, 20 Nov 1995 06:09:47 GMT Organization: Netcom Message-ID: <48p630$avl@ixnews3.ix.netcom.com> References: <32b_9511191940@gigo.com> NNTP-Posting-Host: ix-dc10-18.ix.netcom.com X-NETCOM-Date: Sun Nov 19 10:09:39 PM PST 1995 X-Newsreader: Forte Agent .99b.112