mcdaniel@uicsrd.csrd.uiuc.edu (Tim McDaniel) (06/24/89)
Lars Nyman asked me to copy this over from comp.unix.questions. His site does not receive comp.std.c. If you want to send e-mail, please send it directly to his account shown on the "From:" line. ---------------------------------------------------------------------- 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 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@intellicorp.com -- "Let me control a planet's oxygen supply, and I don't care who makes the laws." - GREAT CTHUHLU'S STARRY WISDOM BAND (via Roger Leroux) __ \ Tim, the Bizarre and Oddly-Dressed Enchanter \ mcdaniel@uicsrd.csrd.uiuc.edu /\ mcdaniel%uicsrd@{uxc.cso.uiuc.edu,uiuc.csnet} _/ \_ {uunet,convex,pur-ee}!uiucuxc!uicsrd!mcdaniel