//接受可变参数的函数实现多个数相加
//不使用库函数
#if 0
#include<stdio.h>
int Add(int num, ...)
{
int sum = 0;
int i = 0;
int* p = (int*)&num + 1;
for (i = 0; i < num; ++i)
{
sum = sum + *p;
p++;
}
return sum;
}
int main(void)
{
int i, j, k;
i = 3;
j = 4;
k = 5;
int sum = Add(3, i, j,k);
printf("%d\n", sum);
return 0;
}
#endif
//利用库函数完成
#include<iostream>
#include<stdarg.h>
using namespace std;
int Add(int num, ...)
{
va_list list;
double sum = 0;
va_start(list, num);
int i;
for (i = 0; i < num; ++i)
{
sum = sum + va_arg(list, int);
}
return sum;
}
int main(void)
{
int i, j, k;
i = 3;
j = 4;
k = 5;
cout << Add(3, i, j, k) << endl;
return 0;
}