man stdarg shows, under the va_args macro description:
If
apis passed to a function that usesva_arg(ap,type), then the value ofapis undefined after the return of that function.
If we look at this following code:
#include <stdio.h>
#include <stdarg.h>
void print_next_arg(va_list ap)
{
printf("%d\n", va_arg(ap, int));
}
void testfunc(int n, ...)
{
va_list ap;
va_start(ap, n);
print_next_arg(ap);
print_next_arg(ap);
va_end(ap);
}
int main(void)
{
testfunc(2, 1, 2);
}
It is initializing a va_list in the testfunc function, sending it to another function which uses va_arg, before returning to the first function and continuing to use the va_list which, according to the man, should have an undefined value by that point.
Is this bad practice? If not, what did the man mean by that quoted line? If so, what would be good practice for utilizing va_arg in a sub-function, and why does my example code run perfectly fine on my machine?