Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: `va_dcl' macro question Message-ID: <9571@smoke.BRL.MIL> Date: 2 Feb 89 23:39:42 GMT References: <1964@kappl.cs.vu.nl> <9507@smoke.BRL.MIL> <991@ubu.warwick.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 49 In article <991@ubu.warwick.UUCP> geoff@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 #if __STDC__ #include #else #include #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 ); }