Elegantly iterating multiple arguments into an array
1 2 3 4 5 6 7
|
void fillSomeArray(int a, int b, int c, int d, int e)
{
for (int i = 0; i < 5; i++)
{
*((*array) + i) = //insert something here
}
}
|
What would be a good way to iterate through my parameters?
Last edited on
I would go with variable argument lists. Plus, you may want to include more parameters later on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <cstdarg>
void assignmentTest(int size, int *array, ...)
{
va_list parameters;
va_start(parameters, size);
for (int i = 0; i < size; i++)
array[i] = va_arg(parameters, int);
va_end(parameters);
}
int main ()
{
int size = 5;
int *x = new int[size];
assignmentTest(size, x, -1, 2, 33, 0, 5);
for (int i = 0; i < size; ++i)
std::cout << x[i] << std::endl;
delete [] x;
return 0;
}
|
Topic archived. No new replies allowed.