nothing against an assignment
.RS
.nf
- va_list aq = ap;
+ va_list aq = ap;
.fi
.RE
Unfortunately, there are also systems that make it an
array of pointers (of length 1), and there one needs
.RS
.nf
- va_list aq;
- *aq = *ap;
+ va_list aq;
+ *aq = *ap;
.fi
.RE
Finally, on systems where parameters are passed in registers,
so that the above assignment can be replaced by
.RS
.nf
- va_list aq;
- va_copy(aq, ap);
- ...
- va_end(aq);
+ va_list aq;
+ va_copy(aq, ap);
+ ...
+ va_end(aq);
.fi
.RE
Each invocation of
#include <stdio.h>
#include <stdarg.h>
-void foo(char *fmt, ...) {
- va_list ap;
- int d;
- char c, *s;
+void
+foo(char *fmt, ...)
+{
+ va_list ap;
+ int d;
+ char c, *s;
- va_start(ap, fmt);
- while (*fmt)
- switch(*fmt++) {
- case 's': /* string */
- s = va_arg(ap, char *);
- printf("string %s\en", s);
- break;
- case 'd': /* int */
- d = va_arg(ap, int);
- printf("int %d\en", d);
- break;
- case 'c': /* char */
- /* need a cast here since va_arg only
- takes fully promoted types */
- c = (char) va_arg(ap, int);
- printf("char %c\en", c);
- break;
- }
- va_end(ap);
+ va_start(ap, fmt);
+ while (*fmt)
+ switch(*fmt++) {
+ case 's': /* string */
+ s = va_arg(ap, char *);
+ printf("string %s\en", s);
+ break;
+ case 'd': /* int */
+ d = va_arg(ap, int);
+ printf("int %d\en", d);
+ break;
+ case 'c': /* char */
+ /* need a cast here since va_arg only
+ takes fully promoted types */
+ c = (char) va_arg(ap, int);
+ printf("char %c\en", c);
+ break;
+ }
+ va_end(ap);
}
.fi
.RE
.nf
#include <varargs.h>
-void foo(va_alist) va_dcl {
- va_list ap;
+void
+foo(va_alist)
+ va_dcl
+{
+ va_list ap;
- va_start(ap);
- while(...) {
- ...
- x = va_arg(ap, type);
- ...
- }
- va_end(ap);
+ va_start(ap);
+ while(...) {
+ ...
+ x = va_arg(ap, type);
+ ...
+ }
+ va_end(ap);
}
.fi
.RE