Here I found an example of how varargs can be used in C.
#include <stdarg.h>
double average(int count, ...)
{
va_list ap;
int j;
double tot = 0;
va_start(ap, count); //Requires the last fixed parameter (to get the address)
for(j=0; j<count; j++)
tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
va_end(ap);
return tot/count;
}
I can understand this example only to some extent.
It is not clear to me why we use va_start(ap, count);
. As far as I understand, in this way we set the iterator to its first element. But why it is not set to the beginning by default?
It is not clear to me why we need to give count
as an argument. Can't C automatically determine the number of the arguments?
It is not clear to me why we use va_end(ap)
. What does it change? Does it set the iterator to the end of the list? But is it not set to the end of the list by the loop? Moreover, why do we need it? We do not use ap
anymore; why do we want to change it?
Remember that arguments are passed on the stack. The va_start
function contains the "magic" code to initialize the va_list
with the correct stack pointer. It must be passed the last named argument in the function declaration or it will not work.
What va_arg
does is use this saved stack pointer, and extract the correct amount of bytes for the type provided, and then modify ap
so it points to the next argument on the stack.
In reality these functions (va_start
, va_arg
and va_end
) are not actually functions, but implemented as preprocessor macros. The actual implementation also depends on the compiler, as different compilers can have different layout of the stack and how it pushes arguments on the stack.