Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!purdue!haven!adm!xadmx!NYMAN@intellicorp.com From: NYMAN@intellicorp.com (Lars Nyman) Newsgroups: comp.unix.questions Subject: variable number of arguments in ANSI C Message-ID: <20070@adm.BRL.MIL> Date: 21 Jun 89 19:48:04 GMT Sender: news@adm.BRL.MIL Lines: 58 What assumptions is OK to make about variable number of arguments in ANSI C ? Example: #include 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@intellicorp.com -------