#include<iostream>
using namespace std;
int sum(int first,int second,...)
{
int num = 0;
int* a;
a = &first;
while(*a != 0)
{
num += *a;
a ++;
}
return num;
}
int main()
{
cout<<sum(1,2,3,0);
}
如下属抄袭:
#include <stdio.h>
int Sum(int first, int second, ...)
{
int sum = 0, t = first;
char * vl;//定义一个指针
vl = (char *)&first;//使指针指向第一个参数
while (*vl != -1)//-1是预先给定的结束符
{
sum += *(int *)vl;//类型转换
vl += sizeof(int);//移动指针,使指针指向下一个参数
}
return sum;
}
int main(int argc, char* argv[])
{
printf("The sum is %d\n", Sum(30, 20, 10, -1));//-1是参数结束标志
return 0;
}