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 ¸Æ¤Ó½Ð¤µ¤ì¤¿´Ø¿ô¦¤Ç¤Ï¸Ä¿ô¤ä·¿¤òÃΤé¤Ê¤¤°ú¤¿ô¤Î¥ê¥¹¥È¤ò¡¢½ç¤Ë°ì
75 ¤Ä¤Å¤ÄÆɤ߹þ¤à¤³¤È¤¬¤Ç¤¤ë¡£
77 .\"O The called function must declare an object of type
79 .\"O which is used by the macros
84 ¸Æ¤Ó½Ð¤µ¤ì¤ë´Ø¿ô¤Ç¤Ï¡¢
86 ·¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬Àë¸À¤µ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£¤³¤Î¥ª¥Ö¥¸¥§¥¯¥È¤¬
90 ¤Î³Æ¥Þ¥¯¥í¤Ë¤è¤Ã¤Æ°·¤ï¤ì¤ë¡£
94 .\"O macro initializes
96 .\"O for subsequent use by
100 .\"O and must be called first.
102 ¥Þ¥¯¥í¤ÏºÇ½é¤Ë¸Æ¤Ó½Ð¤µ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£¤³¤ì¤Ï
108 ¤ÇÍѤ¤¤ë¤³¤È¤¬¤Ç¤¤ë¤è¤¦¤Ë¤¹¤ë¡£
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 ¥Þ¥¯¥í¤Ï¡¢¸Æ¤Ó½Ð¤·»þ¤Ë»ØÄꤵ¤ì¤¿°ú¤¿ô¤Î¤¦¤Á¡¢
141 ¼¡¤Î°ÌÃ֤ˤ¢¤ë¤â¤Î¤ò»ØÄꤷ¤¿·¿
151 ¤Ë¤è¤Ã¤Æ½é´ü²½¤µ¤ì¤Æ¤¤¤ëɬÍפ¬¤¢¤ë¡£
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.
190 ¼¡¤Î°ú¤¿ô¤¬¤Ê¤«¤Ã¤¿¤ê¡¢
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 )
205 ¤Î·Á¤Ç´Ø¿ô¤ËÅϤµ¤ì¤ë¤È¡¢
207 ¤ÎÃͤϴؿô¤«¤éÊ֤äÆÍ褿¸å¤ÏÉÔÄê¤È¤Ê¤ë¡£
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
226 ¤¬¼Â¹Ô¤µ¤ì¤ëËè¤Ë¡¢Æ±¤¸´Ø¿ôÆâ¤ÇÂбþ¤¹¤ë
228 ¤¬¼Â¹Ô¤µ¤ì¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
230 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤¿¸å¡¢ÊÑ¿ô
236 ¤ÎÁȤò²¿²ó¤âʤ٤ƻȤ¦¤³¤È¤â²Äǽ¤Ç¤¢¤ë¡£
238 .\"O may be a macro or a function.
240 ¤Ï¥Þ¥¯¥í¤«¤â¤·¤ì¤Ê¤¤¤·´Ø¿ô¤«¤â¤·¤ì¤Ê¤¤¡£
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
252 ¤³¤Î¤è¤¦¤Ê¾ì¹ç(¤Û¤È¤ó¤É¤Ï¤½¤¦¤Ç¤¢¤ë)¡¢
253 ñ¤Ë°Ê²¼¤Î¤è¤¦¤Ë¤¹¤ì¤Ð¤¤¤¤¤è¤¦¤Ë»×¤¨¤ë¡£
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 ºÇ¸å¤Ë¡¢°ú¤¿ô¤ò¥ì¥¸¥¹¥¿¤ÇÅϤ¹¥·¥¹¥Æ¥à¤Î¾ì¹ç¡¢
285 ¤Ç¥á¥â¥ê¤ò³ä¤êÅö¤Æ¡¢°ú¤¿ô¤ò³ÊǼ¤·¡¢
286 ¼¡¤Î°ú¤¿ô¤¬¤É¤ì¤«¤ò»Ø¤·¼¨¤¹¤è¤¦¤Ë¤¹¤ëɬÍפ¬¤¢¤ë¡£
289 ¤Ç¥ê¥¹¥È¤ò½çÈ֤ˤ¿¤É¤ê¡¢
291 ¤Ç³ä¤êÅö¤Æ¤¿¥á¥â¥ê¤ò³«Êü¤¹¤ë¡£
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.
319 ¤òƱ¤¸´Ø¿ôÆâ¤Ç¼Â¹Ô¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
320 .\"O Some systems that do not supply
324 .\"O instead, since that was the name used in the draft proposal.
325 ¤³¤Î̾Á°¤Ï¤Þ¤À draft proposal ¤Ê¤Î¤Ç¡¢
329 ¤òÍѤ¤¤ë¥·¥¹¥Æ¥à¤â¤¢¤ë¡£
334 ¥Þ¥¯¥í¤Ï C89 ½àµò¤Ç¤¢¤ë¡£
339 ¤Ï C99 ¤ÇÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£
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.
384 ¥Þ¥¯¥í¤Ë \(aq}\(aq ¤ò´Þ¤ß¡¢
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 ¤Î¥³¡¼¥É¤Ë½ñ¤´¹¤¨¤ë¤È¤¤Ë¡¢ÌÌÅݤʺî¶È¤Î¤â¤È¤Ë¤Ê¤ë¡£
414 ¤Þ¤¿¡¢¤¹¤Ù¤Æ¤Î°ú¤¿ô¤ò
416 ¤È¤·¤Æ²ÄÊѸĻØÄꤷ¤¿¤¤¤è¤¦¤Ê¾ì¹ç
418 ¤Ê¤É) ¤Ë¤â¾ã³²¤È¤Ê¤ë¡£
423 ¤Ï½ñ¼°Ê¸»ú¤«¤é¤Ê¤ëʸ»úÎó¤ò¼õ¤±Æþ¤ì¡¢¤½¤Î½ñ¼°Ê¸»ú¤ËÂбþ¤¹¤ë·¿¤Ç²ÄÊѸĤÎ
424 °ú¤¿ô¤òÆɤ߹þ¤ß¡¢°õ»ú¤¹¤ë¡£
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);