147 lines
5.0 KiB
HTML
147 lines
5.0 KiB
HTML
<HTML><HEAD><TITLE><stdarg.h></TITLE></HEAD><BODY BGCOLOR="#FFFFFF">
|
|
|
|
<H1><A NAME="<stdarg.h>"><CODE><stdarg.h></CODE></A></H1><HR>
|
|
|
|
<P><CODE>
|
|
#define <A HREF="#va_arg"><B>va_arg</B></A>(va_list ap,
|
|
<I>T</I>) <I><rvalue of type T></I><BR>
|
|
#define <A HREF="#va_end"><B>va_end</B></A>(va_list ap)
|
|
<I><void expression><\I><BR>
|
|
typedef <I>do-type</I> <A HREF="#va_list"><B>va_list</B></A>;<BR>
|
|
#define <A HREF="#va_start"><B>va_start</B></A>(va_list ap,
|
|
<I>last-par</I>) <I><void expression></I>
|
|
</CODE></P>
|
|
|
|
<P>Include the standard header <B><CODE><stdarg.h></CODE></B>
|
|
to access the unnamed additional arguments
|
|
(arguments with no corresponding parameter declarations)
|
|
in a function that accepts a
|
|
<B><A NAME="varying number of arguments">varying number
|
|
of arguments</A></B>. To access the additional arguments:</P>
|
|
|
|
<UL>
|
|
<LI>The program must first execute the macro
|
|
<A HREF="#va_start"><CODE>va_start</CODE></A> within
|
|
the body of the function to initialize an object with context information.
|
|
|
|
<LI>Subsequent execution of the macro
|
|
<A HREF="#va_arg"><CODE>va_arg</CODE></A>, designating
|
|
the same context information, yields the values of the additional
|
|
arguments in order, beginning with the first unnamed argument. You
|
|
can execute the macro
|
|
<A HREF="#va_arg"><CODE>va_arg</CODE></A>
|
|
from any function that can access
|
|
the context information saved by the macro
|
|
<A HREF="#va_start"><CODE>va_start</CODE></A>.
|
|
|
|
<LI>If you have executed the macro
|
|
<A HREF="#va_start"><CODE>va_start</CODE></A>
|
|
in a function, you must execute the macro
|
|
<A HREF="#va_end"><CODE>va_end</CODE></A>
|
|
in the same function, designating
|
|
the same context information, before the function returns.
|
|
</UL>
|
|
|
|
<P>You can repeat this sequence (as needed) to access the arguments
|
|
as often as you want.</P>
|
|
|
|
<P>You declare an object of type
|
|
<A HREF="#va_list"><CODE>va_list</CODE></A>
|
|
to store context information.
|
|
<A HREF="#va_list"><CODE>va_list</CODE></A>
|
|
can be an array type, which affects how
|
|
the program shares context information with functions that it calls.
|
|
(The address of the first element of an array is passed, rather than
|
|
the object itself.)</P>
|
|
|
|
<P>For example, here is a function
|
|
that concatenates an arbitrary number of strings onto the end of an
|
|
existing string (assuming that the existing string is stored in an
|
|
object large enough to hold the resulting string):</P>
|
|
|
|
<PRE>
|
|
#include <stdarg.h>
|
|
void va_cat(char *s, ...)
|
|
{
|
|
char *t;
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
while (t = va_arg(ap, char *)) <B>null pointer ends list</B>
|
|
{
|
|
s += strlen(s); <B>skip to end</B>
|
|
strcpy(s, t); <B>and copy a string</B>
|
|
}
|
|
va_end(ap);
|
|
}</PRE>
|
|
|
|
<H2><A NAME="va_arg"><CODE>va_arg</CODE></A></H2>
|
|
|
|
<PRE>#define <B>va_arg</B>(va_list ap, <I>T</I>) <I><rvalue of type T></I></PRE>
|
|
|
|
<P>The macro yields the value of the next argument in order, specified
|
|
by the context information designated by <CODE>ap</CODE>. The additional
|
|
argument must be of object type <CODE><I>T</I></CODE>
|
|
after applying the rules for
|
|
<A HREF="function.html#argument promotion" tppabs="http://ccs.ucsd.edu/c/function.html#argument promotion">promoting arguments</A>
|
|
in the absence of a function prototype.</P>
|
|
|
|
<H2><A NAME="va_end"><CODE>va_end</CODE></A></H2>
|
|
|
|
<P><CODE>
|
|
#define <B>va_end</B>(va_list ap) <I><void expression><\I>
|
|
</CODE></P>
|
|
|
|
<P>The macro performs any cleanup necessary,
|
|
after processing the context information designated by
|
|
<CODE>ap</CODE>, so that the function can return.</P>
|
|
|
|
<H2><A NAME="va_list"><CODE>va_list</CODE></A></H2>
|
|
|
|
<P><CODE>
|
|
typedef <I>do-type</I> <B>va_list</B>;
|
|
</CODE></P>
|
|
|
|
<P>The type is the object type <CODE><I>do-type</I></CODE> that you declare
|
|
to hold the context information initialized by
|
|
<A HREF="#va_start"><CODE>va_start</CODE></A> and used by
|
|
<A HREF="#va_arg"><CODE>va_arg</CODE></A>
|
|
to access additional unnamed arguments.</P>
|
|
|
|
<H2><A NAME="va_start"><CODE>va_start</CODE></A></H2>
|
|
|
|
<P><CODE>
|
|
#define <B>va_start</B>(va_list ap, <I>last-par</I>)
|
|
<I><void expression></I>
|
|
</CODE></P>
|
|
|
|
<P>The macro stores initial context information in the object designated
|
|
by <CODE>ap</CODE>. <CODE><I>last-par</I></CODE>
|
|
is the name of the last parameter you
|
|
declare. For example, <CODE><I>last-par</I></CODE>
|
|
is <CODE>b</CODE> for the function
|
|
declared as <CODE>int f(int a, int b, ...)</CODE>. The last parameter must
|
|
not have <CODE>register</CODE> storage class, and it must have a type that
|
|
is not changed by the translator. It cannot have:</P>
|
|
|
|
<UL>
|
|
<LI>an array type
|
|
|
|
<LI>a function type
|
|
|
|
<LI>type <I>float</I>
|
|
|
|
<LI>any integer type that changes when promoted
|
|
</UL>
|
|
|
|
<HR>
|
|
<P>See also the
|
|
<B><A HREF="index.html#Table of Contents" tppabs="http://ccs.ucsd.edu/c/index.html#Table of Contents">Table of Contents</A></B> and the
|
|
<B><A HREF="_index.html" tppabs="http://ccs.ucsd.edu/c/_index.html">Index</A></B>.</P>
|
|
|
|
<P><I>
|
|
<A HREF="crit_pb.html" tppabs="http://ccs.ucsd.edu/c/crit_pb.html">Copyright</A> © 1989-1996
|
|
by P.J. Plauger and Jim Brodie. All rights reserved.</I></P>
|
|
|
|
</BODY></HTML>
|