variable number of arguments in ANSI C
Lars Nyman
NYMAN at intellicorp.com
Thu Jun 22 05:48:04 AEST 1989
What assumptions is OK to make about variable number of arguments in ANSI C ?
Example:
#include <stdarg.h>
void foo(int i, ...)
{
va_list args;
va_start(args, i); /* initialize args pointer */
va_arg(args, char *); /* pop the first argument */
bar(i - 1, args); /* let bar() see the rest, Q1 */
va_start(args, i); /* initialize args pointer again, Q2 */
baz(i, args); /* let baz() see them all, Q1 */
va_end(args); /* done */
}
void bar(int i, va_list args)
{
while (i > 0) {
printf("%s\n", va_arg(args, char *)); /* pop arguments, Q1 */
i--;
}
}
void baz(int i, va_list args)
{
while (i > 0) {
printf("%s\n", va_arg(args, char *)); /* pop arguments, Q1 */
i--;
}
}
main()
{
foo(3, "Monday", "Tuesday", "Wednesday");
}
I know this will work in atleast some implementations of ANSI C, but is it
portable, legal ANSI C code ?
Q1: Is it OK to pass the va_list to another function and let that function pop
all or some of the arguments with va_arg() ?
I believe this is OK according to ANSI, since vprintf() etc. in the
standard library takes a va_list argument. Am I correct ?
Q2: Is it OK to "restart" by calling va_start() more than once ?
Lars Nyman
nyman at intellicorp.com
-------
More information about the Comp.unix.questions
mailing list