Variable argument lists.

Lawrence V. Cipriani lvc at tut.cis.ohio-state.edu
Wed May 11 09:13:16 AEST 1988


In article <14139 at brl-adm.ARPA>, bates%falcon.dnet%fermat at bru.mayo.edu (Cary Bates) writes:
> 
>        Does anybody know (or care) why in ANSI standard C when 
>        using a variable length argument list, there is no way to 
>        determine how many arguments where passed into the function?

In this example (with <varargs.h>) I scan the argument list
twice.  Once to get a count, the next to do the real work.
Something similar should work in ANSI.  Can anyone tell me
if I'm wrong (again! :-( ).  This assumes all the arguments
are char* but it could be changed for a fixed number leading
args and a variable number of other things.

#include <varargs.h>

splat(va_alist)
	va_dcl
{
	va_list ap;
	register unsigned cnt = 0;
	int something;
	char *pi;

	va_start(ap);	/* arg setup */

	while ((pi = va_arg(ap, char *)) != (char *)0)
		cnt++;

	va_end(ap);	/* arg cleanup */

	va_start(ap);	/* arg setup again */

	...whatever...

	va_end(ap);	/* arg cleanup again */
	return something;
}
-- 
Larry Cipriani, AT&T Network Systems and Ohio State University
Domain: lvc at tut.cis.ohio-state.edu
Path: ...!cbosgd!osu-cis!tut.cis.ohio-state.edu!lvc (weird but right)



More information about the Comp.lang.c mailing list