home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16708 < prev    next >
Encoding:
Text File  |  1992-11-17  |  1.9 KB  |  67 lines

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!ames!sun-barr!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
  2. From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Multiple Parameter Passing (unspecified number)
  5. Message-ID: <1992Nov18.075350.1672@thunder.mcrcim.mcgill.edu>
  6. Date: 18 Nov 92 07:53:50 GMT
  7. References: <92321.124834GNR100@psuvm.psu.edu>
  8. Organization: McGill Research Centre for Intelligent Machines
  9. Lines: 57
  10.  
  11. In article <92321.124834GNR100@psuvm.psu.edu>, <GNR100@psuvm.psu.edu> writes:
  12.  
  13. > I'm looking to write a function that accepts any number of
  14. > paramerter.  There will be at least two, an int and a char[].  The
  15. > rest will all be char[]'s.  I need to know how this is done.
  16.  
  17. In C Classic, <varargs.h>; in New C (ie, ANSI), <stdarg.h>.  You will
  18. have to figure out how many arguments there are somehow - perhaps from
  19. the arguments you know are present, perhaps by having some magic value
  20. (nil pointer, for example) as the last argument or something....
  21.  
  22. For example, if we assume that the first arg is the number of extra
  23. arguments, and the second arg is a buffer, and we're supposed to strcat
  24. all the strings together into the buffer, assuming the buffer is big
  25. enough:
  26.  
  27. C Classic:
  28.  
  29.     #include <varargs.h>
  30.  
  31.     void strcatall(va_alist)
  32.     va_dcl
  33.     {
  34.      va_list ap;
  35.      int na;
  36.      char *buf;
  37.     
  38.      va_start(ap);
  39.      na = va_arg(ap,int);
  40.      buf = va_arg(ap,char *);
  41.      buf[0] = '\0';
  42.      for (;na>0;na--) strcat(buf,va_arg(ap,char *));
  43.      va_end(ap);
  44.     }
  45.  
  46. New C:
  47.  
  48.     #include <stdarg.h>
  49.  
  50.     void strcatall(int na, char *buf, ...)
  51.     {
  52.      va_list ap;
  53.     
  54.      va_start(ap,buf);
  55.      buf[0] = '\0';
  56.      for (;na>0;na--) strcat(buf,va_arg(ap,char *));
  57.      va_end(ap);
  58.     }
  59.  
  60. (Of course, there are better ways to do this, like advancing buf and
  61. using strcpy...it's the varargs stuff I'm trying to illustrate, not
  62. string manipulation.)
  63.  
  64.                     der Mouse
  65.  
  66.                 mouse@larry.mcrcim.mcgill.edu
  67.