variable number of arguments

Stephen J. Friedl friedl at vsi.UUCP
Fri Feb 26 15:22:57 AEST 1988


In article <1632 at mhres.mh.nl>, hst at mhres.mh.nl (Klaas Hemstra) writes:
> Try using the vsprintf() function together with varargs.
> Example:
> 
>    MyXText( X, Y, formatString, va_alist)
>    ....
>    va_dcl
>    {   char 	TempString[100];
>        va_list 	args;
>        
>        va_start(args);
>        vsprintf(TempString , formatString, args);
> 
>        va_end(args);
> 
>    }

     I think you are better with:

	MyXText(va_alist)
	va_dcl
	{
	char	buf[256];	/* buffer to format to	*/
	int	x, y;		/* X/Y location		*/
	char	*fmt;		/* printf format string	*/
	va_args	args;

		va_start(args);

		x   = va_arg(args, int);	/* GET...	*/
		y   = va_arg(args, int);	/* .. ALL...	*/
		fmt = va_arg(args, char *);	/* .... ARGS..	*/

		vsprintf(buf, fmt, args);

		va_end(args);
	}

     I have no specific reason to doubt the portability, but it
strikes me that since args are so crazy on various machines (stack
grows up, stack grows down, params passed in registers, etc.)
that letting varargs handle *all* the details would not be a bad
idea.

     Just a thought,
     Steve
-- 
Life : Stephen J. Friedl @ V-Systems, Inc/Santa Ana, CA    *Hi Mom*
CSNet: friedl%vsi.uucp at kent.edu  ARPA: friedl%vsi.uucp at uunet.uu.net
uucp : {kentvax, uunet, attmail, ihnp4!amdcad!uport}!vsi!friedl



More information about the Comp.lang.c mailing list