| |||||
| |||||
![]() |
|||||
|
STDARG
3
2001-10-14
Linux Programmer's Manual
The historic setup is:
#include <varargs.h>
void
foo(va_alist)
va_dcl
{
va_list ap;
va_start(ap);
while (...) {
...
x = va_arg(ap, type);
...
}
va_end(ap);
}
On some systems,
va_end contains a closing (aq}(aq matching a (aq{(aq in
va_start , so that both macros must occur in the same function, and in a way
that allows this.
#include <stdio.h>
#include <stdarg.h>
void
foo(char *fmt, ...)
{
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while (*fmt)
switch (*fmt++) {
case (aqs(aq: /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case (aqd(aq: /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case (aqc(aq: /* char */
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c\n", c);
break;
}
va_end(ap);
}
| |||||
|
| |||||