Mail Archives: djgpp/1997/08/06/10:40:45
Erik Max Francis wrote:
> You don't have set it to anything. Declaring a
> va_list auto variable indicates that you're
> intercepting the arguments to the function (it
> hooks into the stack) and works from there. It
> should be the first declared variable, incidentally.
That is dangerously incorrect. Declaring a variable
with va_list does nothing to "hook into" the stack
at all, it's just a variable (often with type void*,
depending on the compiler, but it can be more complex).
You do have to point it at the first variable argument.
That's what doing
va_start(argc, format);
does, it points the variable at the stack frame. You
also must put
va_end(args);
in the same scope. The variable must also not be accessed
too much within that scope - you can use va_arg(args, type)
to get at each argument in turn, or you can pass it to
another function (like vprintf), but mixing the two is
likely to have odd results (or at least nonportable ones).
However, it can be reset. You can say, for instance,
va_start(args, format);
vprintf(format, args);
va_end(args);
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
to print the same thing to both stdout and stderr, but you
must have the va_end and va_start between them.
There is no need for it to be the first declared variable,
either, it is just an ordinary variable...
Chris C
- Raw text -