.\" Copyright (c) 1990, 1991 The Regents of the University of California. .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the American National Standards Committee X3, on Information .\" Processing Systems. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)stdarg.3 6.8 (Berkeley) 6/29/91 .\" .\" Converted for Linux, Mon Nov 29 15:11:11 1993, faith@cs.unc.edu .\" Additions, 2001-10-14, aeb .\" .\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved. .\" Translated Fri Mar 22 1998 by NAKANO Takeo .\" Updated Tue Oct 16 2001 by Kentaro Shirakata .\" .TH STDARG 3 2001-10-14 "" "Linux Programmer's Manual" .SH 名前 stdarg, va_start, va_arg, va_end, va_copy \- 個数・型が可変な引数リスト .SH 書式 .B #include .sp .BI "void va_start(va_list " ap ", " last ); .br .IB type " va_arg(va_list " ap ", " type ); .br .BI "void va_end(va_list " ap ); .br .BI "void va_copy(va_list " dest ", va_list " src ); .\"O .SH DESCRIPTION .SH 説明 .\"O A function may be called with a varying number of arguments of varying .\"O types. .\"O The include file .\"O .I .\"O declares a type .\"O .I va_list .\"O and defines three macros for stepping through a list of arguments whose .\"O number and types are not known to the called function. 関数は呼び出しに際して、個数や型が可変な引数をとることができる。 インクルードファイル .I では .I va_list 型が宣言されており、3 つのマクロが定義されている。これらを用いると、 呼び出された関数側では個数や型を知らない引き数のリストを、順に一 つづつ読み込むことができる。 .PP .\"O The called function must declare an object of type .\"O .I va_list .\"O which is used by the macros .\"O .BR va_start (), .\"O .BR va_arg (), .\"O and .\"O .BR va_end (). 呼び出される関数では、 .I va_list 型のオブジェクトが宣言されていなければならない。このオブジェクトが .BR va_start (), .BR va_arg (), .BR va_end () の各マクロによって扱われる。 .SS va_start() .\"O The .\"O .BR va_start () .\"O macro initializes .\"O .I ap .\"O for subsequent use by .\"O .BR va_arg () .\"O and .\"O .BR va_end (), .\"O and must be called first. .BR va_start () マクロは最初に呼び出さなければならない。これは .I ap を初期化し、 .BR va_arg () と .BR va_end () で用いることができるようにする。 .PP .\"O The argument .\"O .I last .\"O is the name of the last argument before the variable argument list, that is, .\"O the last argument of which the calling function knows the type. 引き数 .I last は引き数リストのうち、可変な部分の直前に置かれる引き数の名前であ る。つまり呼び出された関数が型を知っている最後の引き数である。 .PP .\"O Because the address of this argument may be used in the .\"O .BR va_start () .\"O macro, it should not be declared as a register variable, .\"O or as a function or an array type. この引き数はレジスタ変数や関数、配列として 宣言してはならない。この引き数のアドレスが .BR va_start () マクロで用いられるかもしれないからである。 .SS va_arg() .\"O The .\"O .BR va_arg () .\"O macro expands to an expression that has the type and value of the next .\"O argument in the call. .\"O The argument .\"O .I ap .\"O is the .\"O .I va_list .\"O .I ap .\"O initialized by .\"O .BR va_start (). .BR va_arg () マクロは、呼び出し時に指定された引き数のうち、 次の位置にあるものを指定した型 .I type の値として取得する。 引き数 .I ap は .I va_list .I ap で、 .BR va_start () によって初期化されている必要がある。 .\"O Each call to .\"O .BR va_arg () .\"O modifies .\"O .I ap .\"O so that the next call returns the next argument. .\"O The argument .\"O .I type .\"O is a type name specified so that the type of a pointer to an object that .\"O has the specified type can be obtained simply by adding a * to .\"O .IR type . .BR va_arg () を呼び出すごとに .I ap は変更され、次回の呼び出しの際に、さらに次の引き数を返すようになる。 引き数 .I type は型の名前である。 .I type の前に * を付ければ、オブジェクトへの型付きポインタが得られる。 .PP .\"O The first use of the .\"O .BR va_arg () .\"O macro after that of the .\"O .BR va_start () .\"O macro returns the argument after .\"O .IR last . .\"O Successive invocations return the values of the remaining arguments. .BR va_start () マクロの直後に .BR va_arg () を最初に実行すると、 .I last の次の引き数が返る。続けて実行すると、残りの引き数がそれぞれ返る。 .PP .\"O If there is no next argument, or if .\"O .I type .\"O is not compatible with the type of the actual next argument (as promoted .\"O according to the default argument promotions), random errors will occur. 次の引き数がなかったり、 .I type が次の引き数の実際の型と互換でない場合 (デフォルトの引き数変換で扱 えなかった場合) には、予測できないエラーが起こる。 .PP .\"O If .\"O .I ap .\"O is passed to a function that uses .\"O .BI va_arg( ap , type ) .\"O then the value of .\"O .I ap .\"O is undefined after the return of that function. .I ap が .BI va_arg( ap , type ) の形で関数に渡されると、 .I ap の値は関数から返って来た後は不定となる。 .SS va_end() .\"O Each invocation of .\"O .BR va_start () .\"O must be matched by a corresponding invocation of .\"O .BR va_end () .\"O in the same function. .\"O After the call .\"O .BI va_end( ap ) .\"O the variable .\"O .I ap .\"O is undefined. .\"O Multiple traversals of the list, each .\"O bracketed by .\"O .BR va_start () .\"O and .\"O .BR va_end () .\"O are possible. .BR va_start () が実行される毎に、同じ関数内で対応する .BR va_end () が実行されなければならない。 .BI va_end( ap ) が呼び出された後、変数 .I ap の値は不定となる。 .BR va_start () と .BR va_end () の組を何回も並べて使うことも可能である。 .\"O .BR va_end () .\"O may be a macro or a function. .BR va_end () はマクロかもしれないし関数かもしれない。 .SS va_copy() .\"O .\" Proposal from clive@demon.net, 1997-02-28 .\"O An obvious implementation would have a .\"O .I va_list .\"O be a pointer to the stack frame of the variadic function. すぐ分かる .I va_list の実装は、variadic な関数のスタックフレームのポインタである。 .\"argrath: variadic? .\"O In such a setup (by far the most common) there seems .\"O nothing against an assignment このような場合(ほとんどはそうである)、 単に以下のようにすればいいように思える。 .in +4n .nf va_list aq = ap; .fi .in .\"O Unfortunately, there are also systems that make it an .\"O array of pointers (of length 1), and there one needs 残念ながら、(長さ 1の)ポインタの配列として扱うシステムもある。 そのような場合、以下のようにする必要がある。 .in +4n .nf va_list aq; *aq = *ap; .fi .in .\"O Finally, on systems where arguments are passed in registers, .\"O it may be necessary for .\"O .BR va_start () .\"O to allocate memory, store the arguments there, and also .\"O an indication of which argument is next, so that .\"O .BR va_arg () .\"O can step through the list. .\"O Now .\"O .BR va_end () .\"O can free the allocated memory again. 最後に、引き数をレジスタで渡すシステムの場合、 .BR va_start () でメモリを割り当て、引き数を格納し、 次の引き数がどれかを指し示すようにする必要がある。 そして .BR va_arg () でリストを順番にたどり、 .BR va_end () で割り当てたメモリを開放する。 .\"O To accommodate this situation, C99 adds a macro .\"O .BR va_copy (), .\"O so that the above assignment can be replaced by このような状況に対応するため、C99 では .BR va_copy () マクロを追加し、 前述のような割り当ては以下のように置き換えられるようにした。 .in +4n .nf va_list aq; va_copy(aq, ap); \&... va_end(aq); .fi .in .RE .\"O Each invocation of .\"O .BR va_copy () .\"O must be matched by a corresponding invocation of .\"O .BR va_end () .\"O in the same function. .BR va_copy () が実行されるごとに、 対応する .BR va_end () を同じ関数内で実行しなければならない。 .\"O Some systems that do not supply .\"O .BR va_copy () .\"O have .\"O .B __va_copy .\"O instead, since that was the name used in the draft proposal. この名前はまだ draft proposal なので、 .BR va_copy () の代わりに .B __va_copy を用いるシステムもある。 .SH 準拠 .BR va_start (), .BR va_arg (), .BR va_end () マクロは C89 準拠である。 .\"O C99 defines the .\"O .BR va_copy () .\"O macro. .BR va_copy () は C99 で定義されている。 .\"O .SH NOTES .SH 注意 .\"O These macros are .\"O .I not .\"O compatible with the historic macros they replace. .\"O A backward-compatible version can be found in the include file .\"O .IR . これらのマクロは、以前から用いられてきた同等のマクロ群と 互換では\fIない\fP。過去のものと互換なバージョンは、 インクルードファイル .I に存在する。 .PP .\"O The historic setup is: 歴史的なセットアップは以下のとおりである。 .in +4n .nf #include void foo(va_alist) va_dcl { va_list ap; va_start(ap); while (...) { ... x = va_arg(ap, type); ... } va_end(ap); } .fi .in .\"O On some systems, .\"O .I va_end .\"O contains a closing \(aq}\(aq matching a \(aq{\(aq in .\"O .IR va_start , .\"O so that both macros must occur in the same function, and in a way .\"O that allows this. .I va_start マクロに \(aq}\(aq を含み、 .I va_end マクロに対応する \(aq{\(aq を含むシステムもあるので、 この二つのマクロは同じ関数になければならない。 .\"O .SH BUGS .SH バグ .\"O Unlike the .\"O .B varargs .\"O macros, the .\"O .B stdarg .\"O macros do not permit programmers to code a function with no fixed .\"O arguments. .\"O This problem generates work mainly when converting .\"O .B varargs .\"O code to .\"O .B stdarg .\"O code, but it also creates difficulties for variadic functions that wish to .\"O pass all of their arguments on to a function that takes a .\"O .I va_list .\"O argument, such as .\"O .BR vfprintf (3). .B varargs マクロとは異なり、 .B stdarg マクロでは固定引き数なしで関数を指定することが許されていない。 これは .B varargs ベースのコードを .B stdarg のコードに書き換えるときに、面倒な作業のもとになる。 また、すべての引き数を .I va_list として可変個指定したいような場合 .RB ( vfprintf (3) など) にも障害となる。 .\"O .SH EXAMPLE .SH 例 関数 .I foo は書式文字からなる文字列を受け入れ、その書式文字に対応する型で可変個の 引き数を読み込み、印字する。 .nf #include #include void foo(char *fmt, ...) { va_list ap; int d; char c, *s; va_start(ap, fmt); while (*fmt) switch (*fmt++) { case \(aqs\(aq: /* string */ s = va_arg(ap, char *); printf("string %s\en", s); break; case \(aqd\(aq: /* int */ d = va_arg(ap, int); printf("int %d\en", d); break; case \(aqc\(aq: /* char */ /* need a cast here since va_arg only takes fully promoted types */ c = (char) va_arg(ap, int); printf("char %c\en", c); break; } va_end(ap); } .fi