

variable arguments           Overview          variable arguments




The ANSI  Standard mandates the creation of a  set of routines to
help implement  functions, such as pprriinnttff and  ssccaannff, that take a
variable  number of  arguments.  These  routines are  called from
within another function to help it handle its arguments.

These routines  are declared or  defined in the  header ssttddaarrgg.hh,
and are as follows:


     vvaa_aarrgg()  Return pointer to next argument in argument list
     vvaa_eenndd()  Tidy up after an argument list has been traversed
     vvaa_ssttaarrtt()Initialize object that holds function arguments


vvaa_aarrgg and vvaa_ssttaarrtt must be implemented as macros; vvaa_eenndd must be
implemented  as a  library function.  All  three use  the special
type vvaa_lliisstt, which is an  object that holds the arguments to the
function being implemented.

***** Example *****

The following example concatenates multiple strings into a common
allocated string and returns the string's address.


#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>



char *
multcat(numargs)
int numargs;
{
          va_list argptr;
          char *result;
          int i, siz;



          /* get size required */
          va_start(argptr, numargs);
          for(siz = i = 0; i < numargs; i++)
          siz += strlen(va_arg(argptr, char *));



          if ((result = calloc(siz + 1, 1)) == NULL) {
          fprintf(stderr, "Out of space\n");
          exit(EXIT_FAILURE);
          }
          va_end(argptr);


COHERENT Lexicon                                           Page 1




variable arguments           Overview          variable arguments






          va_start(argptr, numargs);
          for(i = 0; i < numargs; i++)
          strcat(result, va_arg(argptr, char *));
          va_end(argptr);
          return(result);
}



int
main(void)
{
          printf(multcat(5, "One ", "two ", "three ",
          "testing", ".\n"));
}


***** See Also *****

lliibbrraarriieess, ssttddaarrgg.hh


































COHERENT Lexicon                                           Page 2


