编译环境:Vs2012;
//Handling a variable number of arguments
/////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include<stdlib.h>
#include<cstdarg>
using std::cout;
using std::endl;
int sum (int count,...)
{
if (count<=0)
return 0;
va_list arg_ptr;
va_start(arg_ptr,count);
int sum=0;
for (int i=0;i<count;i++)
sum +=va_arg(arg_ptr,int);
va_end(arg_ptr);
return sum;
}
int main(int argc,char* argv[])
{
cout<<sum(3,4,2,4,6,6,7,7,5,5,4,4,3,5,6,5,3,3,3)<<endl;
cout<<sum(32,34,23,45,23,46,75,34,23,12,35)<<endl;
system("pause");
return 0;
}
输出结果不正确,问题出在哪里了呢 。分析中。。。
时间: 2024-11-10 01:25:51