测试代码:
#include <bits/stdc++.h>
#include <stdarg.h>
using namespace std;
void print(int nums, ...)
{
va_list ap;
va_start(ap, nums);
va_end(ap);
for (int i = 0; i < nums; ++i)
{
int a = va_arg(ap, int);
cout << a << " ";
}
cout << endl;
}
int main(int argc, char **argv)
{
print(3, 1, 2, 3);
return 0;
}
测试结果:
$ ./a.out
1 2 3