The following declares the new type va_list at the start.
Then between va_start and va_end we can access arguments using va_args repeatedly, while giving it the type to take out.
void my_function(int count, ...) {
va_list args;
int i;
va_start(args, count);
for (i = 0; i < count; i++) {
int num = va_arg(args, int);
}
va_end(args);
}
When we traverse va_arg we can’t go back. Another approach is to use va_copy to take a copy of the args before going through them.
void my_function(int count, ...) {
va_list args;
int i;
va_start(args, count);
va_copy(args_copy, args);
for (i = 0; i < count; i++) {
int num = va_arg(args, int);
}
for (i = 0; i < count; i++) {
int num = va_arg(args_copy, int);
}
va_end(args);
va_end(args_copy);
}