Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!wuarchive!decwrl!ucbvax!ucsfcgl!pixar!rta From: rta@pixar.UUCP (Rick Ace) Newsgroups: comp.lang.c Subject: ANSI C stdarg questions Keywords: stdarg ANSI C Message-ID: <8217@pixar.UUCP> Date: 21 Dec 89 01:09:01 GMT Organization: Pixar -- Marin County, California Lines: 71 Consider the following ANSI C program: ---------------------------------------------------------------------- #include /* print each number in the argument list of test(), stop at 0 */ void print1(va_list ap) { int i; while (i = va_arg(ap, int)) printf("%d\n", i); } /* print the sum of the numbers in the argument list of test(), stop at 0 */ void print2(va_list ap) { int sum = 0, i; while (i = va_arg(ap, int)) sum += i; printf("sum %d\n", sum); } /* argument "x" is not used, but must be present for stdarg.h */ void test(int x, ...) { va_list ap; va_start(ap, x); print1(ap); print2(ap); va_end(x); } main(int argc, char **argv) { test(0, 1, 5, 7, 0); } ---------------------------------------------------------------------- This program works on two different ANSI implementations (the NeXT and the SGI Iris). That, of course, does not guarantee conformance to the standard, and K&R 2E doesn't address all the details, so here are two questions: 1) Is it legal ANSI C for test() to pass the argument "ap" to print1() and print2() as it does in the above program? 2) Is it legal ANSI C to traverse the argument list multiple times? For example, int sum; va_list ap; va_start(ap, x); /* start first traversal */ sum = 0; while (i = va_arg(ap, int)) printf("%d\n", i); va_end(ap); va_start(ap, x); /* start second traversal */ sum = 0; while (i = va_arg(ap, int)) sum += i; va_end(ap); printf("sum %d\n", sum); Please answer both questions. No partial credit will be given :-) Rick Ace {sun,ucbvax}!pixar!rta