Barguast - In the va_start macro, the a parameter is intended to let the compiler know where the argument list starts. It all has to do with the C calling convention: the arguments are all adjacent in memory. Now, when you pass by reference, the a you give to the macro is
not in the right place, but instead points to the original location of the variable you passed (here, the temporary that the compiler generated to hold the literal 5). The memory location that would have held a in the argument list will instead hold the address of a (as if it were a pointer), but the compiler automatically adds a dereference when it is used.
So basically, the code you wrote is equivalent to:
void testfunc (const int* a, ...){ va_list Params; va_start (Params, *a); cout << va_arg (Params, int); va_end (Params);}void main(){ int x = 5; testfunc (&x, 6);}
From *a there is no way you can find the remaining parameters.
Remember that varargs are a
C library utility. Not a C++ one.
Consequences:
a) Don't pass by reference when using varargs. If really necessary, take the address of the variable and pass by pointer.
b) You
cannot access (non-POD) object types through the va_arg macro.
[edited by - Fruny on April 23, 2004 10:51:21 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan