`va_dcl' macro question
Doug Gwyn
gwyn at smoke.BRL.MIL
Fri Feb 3 09:39:42 AEST 1989
In article <991 at ubu.warwick.UUCP> geoff at emerald.UUCP (Geoff Rimmer) writes:
>All I know about variable arguments in ANSI C is how to do prototypes,
>and define the functions. However, I don't know how to read each of
>the arguments passed to the function. Could someone email/post a
>simple function that will take a variable number of strings, and then
>print them to stdout one at a time.
>i.e.
> void print_strings (char *str, ...)
> {
> ...
> }
Well, there has to be some way to tell when the argument list is
exhausted, which you didn't specify. Let's assume that a null pointer
marks the end of the list. Then here is how I would implement this:
#include <stdio.h>
#if __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#define const /* nothing */
#endif
#if __STDC__
void /* DESIGN BOTCH: should return status */
print_strings( register const char *str, ... )
#else
void
print_strings( va_alist )
va_dcl
#endif
{
#if !__STDC__
register const char *str;
#endif
va_list ap;
#if __STDC__
va_start( ap, str );
#else
va_start( ap );
str = va_arg( ap, const char * );
#endif
for ( ; str != NULL; str = va_arg( ap, const char * ) )
(void)fputs( str, stdout );
va_end( ap );
}
More information about the Comp.lang.c
mailing list