1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)stdarg.3 6.8 (Berkeley) 6/29/91
38 .\" Converted for Linux, Mon Nov 29 15:11:11 1993, faith@cs.unc.edu
39 .\" Additions, 2001-10-14, aeb
41 .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
42 .\" Translated Fri Mar 22 1998 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
43 .\" Updated Tue Oct 16 2001 by Kentaro Shirakata <argrath@ub32.org>
45 .TH STDARG 3 2001-10-14 "" "Linux Programmer's Manual"
47 stdarg, va_start, va_arg, va_end, va_copy \- 個数・型が可変な引数リスト
49 .B #include <stdarg.h>
51 .BI "void va_start(va_list " ap ", " last );
53 .IB type " va_arg(va_list " ap ", " type );
55 .BI "void va_end(va_list " ap );
57 .BI "void va_copy(va_list " dest ", va_list " src );
60 .\"O A function may be called with a varying number of arguments of varying
66 .\"O and defines three macros for stepping through a list of arguments whose
67 .\"O number and types are not known to the called function.
68 関数は呼び出しに際して、個数や型が可変な引数をとることができる。
73 型が宣言されており、3 つのマクロが定義されている。これらを用いると、
74 呼び出された関数側では個数や型を知らない引き数のリストを、順に一
77 .\"O The called function must declare an object of type
79 .\"O which is used by the macros
86 型のオブジェクトが宣言されていなければならない。このオブジェクトが
94 .\"O macro initializes
96 .\"O for subsequent use by
100 .\"O and must be called first.
102 マクロは最初に呼び出さなければならない。これは
112 .\"O is the name of the last argument before the variable argument list, that is,
113 .\"O the last argument of which the calling function knows the type.
116 は引き数リストのうち、可変な部分の直前に置かれる引き数の名前であ
117 る。つまり呼び出された関数が型を知っている最後の引き数である。
119 .\"O Because the address of this argument may be used in the
121 .\"O macro, it should not be declared as a register variable,
122 .\"O or as a function or an array type.
123 この引き数はレジスタ変数や関数、配列として
124 宣言してはならない。この引き数のアドレスが
126 マクロで用いられるかもしれないからである。
130 .\"O macro expands to an expression that has the type and value of the next
131 .\"O argument in the call.
138 .\"O .BR va_start ().
140 マクロは、呼び出し時に指定された引き数のうち、
156 .\"O so that the next call returns the next argument.
159 .\"O is a type name specified so that the type of a pointer to an object that
160 .\"O has the specified type can be obtained simply by adding a * to
165 は変更され、次回の呼び出しの際に、さらに次の引き数を返すようになる。
170 の前に * を付ければ、オブジェクトへの型付きポインタが得られる。
172 .\"O The first use of the
174 .\"O macro after that of the
176 .\"O macro returns the argument after
178 .\"O Successive invocations return the values of the remaining arguments.
184 の次の引き数が返る。続けて実行すると、残りの引き数がそれぞれ返る。
186 .\"O If there is no next argument, or if
188 .\"O is not compatible with the type of the actual next argument (as promoted
189 .\"O according to the default argument promotions), random errors will occur.
192 が次の引き数の実際の型と互換でない場合 (デフォルトの引き数変換で扱
193 えなかった場合) には、予測できないエラーが起こる。
197 .\"O is passed to a function that uses
198 .\"O .BI va_arg( ap , type )
199 .\"O then the value of
201 .\"O is undefined after the return of that function.
204 .BI va_arg( ap , type )
209 .\"O Each invocation of
211 .\"O must be matched by a corresponding invocation of
213 .\"O in the same function.
215 .\"O .BI va_end( ap )
219 .\"O Multiple traversals of the list, each
238 .\"O may be a macro or a function.
242 .\"O .\" Proposal from clive@demon.net, 1997-02-28
243 .\"O An obvious implementation would have a
245 .\"O be a pointer to the stack frame of the variadic function.
248 の実装は、variadic な関数のスタックフレームのポインタである。
249 .\"argrath: variadic?
250 .\"O In such a setup (by far the most common) there seems
251 .\"O nothing against an assignment
261 .\"O Unfortunately, there are also systems that make it an
262 .\"O array of pointers (of length 1), and there one needs
263 残念ながら、(長さ 1の)ポインタの配列として扱うシステムもある。
264 そのような場合、以下のようにする必要がある。
273 .\"O Finally, on systems where arguments are passed in registers,
274 .\"O it may be necessary for
276 .\"O to allocate memory, store the arguments there, and also
277 .\"O an indication of which argument is next, so that
279 .\"O can step through the list.
282 .\"O can free the allocated memory again.
283 最後に、引き数をレジスタで渡すシステムの場合、
286 次の引き数がどれかを指し示すようにする必要がある。
292 .\"O To accommodate this situation, C99 adds a macro
294 .\"O so that the above assignment can be replaced by
295 このような状況に対応するため、C99 では
298 前述のような割り当ては以下のように置き換えられるようにした。
310 .\"O Each invocation of
312 .\"O must be matched by a corresponding invocation of
314 .\"O in the same function.
320 .\"O Some systems that do not supply
324 .\"O instead, since that was the name used in the draft proposal.
325 この名前はまだ draft proposal なので、
342 .\"O These macros are
344 .\"O compatible with the historic macros they replace.
345 .\"O A backward-compatible version can be found in the include file
346 .\"O .IR <varargs.h> .
347 これらのマクロは、以前から用いられてきた同等のマクロ群と
348 互換では\fIない\fP。過去のものと互換なバージョンは、
353 .\"O The historic setup is:
354 歴史的なセットアップは以下のとおりである。
369 x = va_arg(ap, type);
377 .\"O On some systems,
379 .\"O contains a closing \(aq}\(aq matching a \(aq{\(aq in
381 .\"O so that both macros must occur in the same function, and in a way
382 .\"O that allows this.
386 マクロに対応する \(aq{\(aq を含むシステムもあるので、
387 この二つのマクロは同じ関数になければならない。
394 .\"O macros do not permit programmers to code a function with no fixed
396 .\"O This problem generates work mainly when converting
400 .\"O code, but it also creates difficulties for variadic functions that wish to
401 .\"O pass all of their arguments on to a function that takes a
403 .\"O argument, such as
404 .\"O .BR vfprintf (3).
408 マクロでは固定引き数なしで関数を指定することが許されていない。
413 のコードに書き換えるときに、面倒な作業のもとになる。
423 は書式文字からなる文字列を受け入れ、その書式文字に対応する型で可変個の
440 case \(aqs\(aq: /* string */
441 s = va_arg(ap, char *);
442 printf("string %s\en", s);
444 case \(aqd\(aq: /* int */
446 printf("int %d\en", d);
448 case \(aqc\(aq: /* char */
449 /* need a cast here since va_arg only
450 takes fully promoted types */
451 c = (char) va_arg(ap, int);
452 printf("char %c\en", c);