可变参函数的使用,怎么确定结束位置
可变参函数的使用,如何确定结束位置?

------解决方案--------------------
用一个特殊的值,或者传入参数个数.
------解决方案--------------------
va_arg, va_end, va_start
Access variable-argument lists.
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
void va_start( va_list arg_ptr ); (UNIX version)
void va_start( va_list arg_ptr, prev_param ); (ANSI version)
Routine Required Header Optional Headers Compatibility
va_arg <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
va_end <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
va_start <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
1 Required for UNIX V compatibility.
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
va_arg returns the current argument; va_start and va_end do not return values.
Parameters
type
Type of argument to be retrieved
arg_ptr
Pointer to list of arguments
prev_param
Parameter preceding first optional argument (ANSI only)
Remarks
The va_arg, va_end, and va_start macros provide a portable way to access the arguments to a function when the function takes a variable number of arguments. Two versions of the macros are available: The macros defined in STDARG.H conform to the ANSI C standard, and the macros defined in VARARGS.H are compatible with the UNIX System V definition. The macros are:
//---------------------------------------------------------------------------
#include <iostream>
#include <stdarg.h>
#pragma hdrstop
using namespace std;
//---------------------------------------------------------------------------
//定义一个可变参函数
//功能:求参数列表的所有参数的平方和
int SqSum(int num1, ...)//可变参数用"..."来表示
{
va_list arg_ptr = NULL; //定义指向可变参数列表的指针
int nSum = 0,temp_num = num1;
va_start(arg_ptr, num1); //让指针指向函数参数列表的最后一个固定参数
while(temp_num != 0) //输入时把0当作结束标志,没想到好办法,
//列表中有0就完蛋了,不能正确输出结果
{
nSum += (temp_num * temp_num);
cout << nSum << endl;
temp_num = va_arg(arg_ptr, int); //获取参数列表的参数,强转为int型
}
va_end(arg_ptr); //结束标志,清空参数列表
return nSum;
}
#pragma argsused
int main(int argc, char* argv[])
{
int sqsum = SqSum(2, 100, -5, 68, 34, 12, 70, 110, -26,
7, 99, 3, 8, 32, 44, 289, 1111, 0);
cout << "sqsum = " << sqsum << endl;
cin.get();
return 0;
}
------解决方案--------------------
用一个特殊的值,或者传入参数个数.
------解决方案--------------------
va_arg, va_end, va_start
Access variable-argument lists.
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
void va_start( va_list arg_ptr ); (UNIX version)
void va_start( va_list arg_ptr, prev_param ); (ANSI version)
Routine Required Header Optional Headers Compatibility
va_arg <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
va_end <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
va_start <stdio.h> and <stdarg.h> <varargs.h>1 ANSI, Win 95, Win NT
1 Required for UNIX V compatibility.
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
va_arg returns the current argument; va_start and va_end do not return values.
Parameters
type
Type of argument to be retrieved
arg_ptr
Pointer to list of arguments
prev_param
Parameter preceding first optional argument (ANSI only)
Remarks
The va_arg, va_end, and va_start macros provide a portable way to access the arguments to a function when the function takes a variable number of arguments. Two versions of the macros are available: The macros defined in STDARG.H conform to the ANSI C standard, and the macros defined in VARARGS.H are compatible with the UNIX System V definition. The macros are: