Passing variable #of args

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Jan 22 00:16:23 AEST 1989


In article <470 at marob.MASA.COM> daveh at marob.masa.com (Dave Hammond) writes:
>What potential problems exist if A() passes *more* than 3 args to B()?
>It seems to me that this would harm nothing, since the extra stuff
>ends up beyond the current stack position.

Stack?  What stack?  The C language doesn't say anything about a stack.

In fact there are reasonable implementations that
	(a) pass arguments differently for different parameter types
	(b) use different mechanisms for passing different numbers
	    of arguments.

>I was surprised to see the following in a "GNU-something" source file:
>lprintf(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,10)
>char *fmt;
>{
>	char buf[BUFSIZ];
>	sprintf(buf,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,10)
>}
>Given the availability of varargs, is this style still acceptable and,
>more importantly, is it portable?

That style never WAS acceptable; it relies on implementation-dependent
kludgery and will not work on some implementations.  Before there was
<varargs.h>, this technique was resorted to fairly heavily because it
happened to work (most of the time) on the local implementation and
there was no better recourse.  The proper way to do this is with
<varargs.h> or <stdarg.h> macros, passing the va_list to vsprintf().
Of course, some systems don't have vsprintf(), but the solution there
is to implement vsprintf() (it's just a few lines of code) and add it
to the local set of C library extensions.



More information about the Comp.lang.c mailing list