Hi folks,
I am experimenting with variable argument lists and seeing some strange results...
The piece of code I am testing is:
#include <stdio.h>
#include <stdarg.h>
void foo(int param1, int param2, ...)
{
int param3 = 0;
va_list ap;
va_start(ap, param2);
param3 = va_arg(ap, int);
va_end(ap);
printf("param3: %d\n", param3);
}
int main(void)
{
foo(1,1);
foo(1,1,42);
}
And the output for that snippet is:
param3: -1073748472
param3: 42
For the second call: 'foo(1,1,42)', everything seems to work as expected.
For the first call: 'foo(1,1)', the result look like an uninitialised int, although I do set it to 0 when I first initialise it at the beginning of the function.
I would like to be able to rely on the fact that the resultant variable should have the value 0 if the argument is not called. I would have thought va_arg() would be sensible enough to deal with that but it doesn't seem to be the case.
Any suggestions to deal with that?
Many thanks.