variable number of arguments

Klaas Hemstra hst at mhres.mh.nl
Tue Feb 23 18:30:16 AEST 1988


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);

   }

Note that you have to include the varargs.h file. This file is present in most
Unix systems and also with most non-Unix C compilers.

Be carefull using varargs !! Recently I had some trouble using them. 
If you think they are REALLY flexible you are wrong.
e.g.: You have a function accepting a variable number of arguments with
the va_alist declaration. Then you can call this function with (..,a1,a2,...).
But if you want to call the function from another function which also accepts
a variable number of arguments, you can NOT pass that variable number of
arguments to the first function mentioned. This is probably the reason
why vprintf,vfprintf & vsprintf exist.

Note that on some of the Unix systems I tried this was possible but not on all
of them ! (I don't want a discussion on where you can and where you can't use
this way of varargs passing).



More information about the Comp.lang.c mailing list