va_arg
これは、以下の評判の高いリンクで述べられていることです。
http://www.cplusplus.com/reference/cstdarg/va_arg/
Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the number of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read.
それに加えて、私が読んだまあまあの本va_arg
では、すべての例で、fixed
引数の1つが常に、渡す変数引数の数/カウントであることを確認しています.そして、このカウントはループで使用されます次の項目に進み、変数リストの最後の引数を取得するva_arg
ときにループ条件 (カウントを使用) が終了することを確認します。va_arg
"the function should be designed in such a way........" (above)
率直に言えば、va_arg
はちょっとばかげています。しかし、その Web サイトから取った次の例ではva_end
、 はva_arg
突然スマートに動作するように見えます。タイプの変数引数リストの最後にchar*
到達すると、NULL ポインターを返します。どうですか?私がリンクした一番上の段落は明確に述べています
"va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list"
さらに、次のプログラムには、可変引数リストの末尾を越えたときに NULL ポインターが返されることを保証するものは何もありませんva_arg
。
/* va_end example */
#include <stdio.h> /* puts */
#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
void PrintLines (char* first, ...)
{
char* str;
va_list vl;
str=first;
va_start(vl,first);
do {
puts(str);
str=va_arg(vl,char*);
} while (str!=NULL);
va_end(vl);
}
int main ()
{
PrintLines ("First","Second","Third","Fourth",NULL);
return 0;
}
出力
First
Second
Third
Fourth