home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15993 < prev    next >
Encoding:
Text File  |  1992-11-09  |  1.8 KB  |  48 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!cs.utexas.edu!swrinde!gatech!news.ans.net!newsgate.watson.ibm.com!yktnews!admin!siena!mittle
  3. From: mittle@watson.ibm.com (Josh Mittleman)
  4. Subject: Re: Referencing `...' arguments in functions
  5. Sender: news@watson.ibm.com (NNTP News Poster)
  6. Message-ID: <1992Nov09.211314.162751@watson.ibm.com>
  7. Date: Mon, 09 Nov 1992 21:13:14 GMT
  8. Distribution: usa
  9. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  10. References:  <b#n125j@rpi.edu>
  11. Nntp-Posting-Host: siena.watson.ibm.com
  12. Organization: IBM T.J. Watson Research Center
  13. Lines: 33
  14.  
  15.  
  16. > I wish to write a function having an unknown number of arguments.  I know
  17. > that the '...' in the function declaration will allow for any number of
  18. > arguments, but how do I actually access these variables.
  19.  
  20. According to ARM, p.146, the only guaranteed way to access the arguments
  21. passed using ... is the va_list macros in stdarg.h.  They are used as
  22. follows: 
  23.  
  24.   void f(int n, ...) {
  25.     va_list theList;
  26.     va_start(theList, n);
  27.     for (int i = 0; i < n; i++)
  28.       int j = va_arg(theList, int);
  29.       cout << j << endl;
  30.     va_end(theList);
  31.   }
  32.  
  33. This function prints out a list of the n int arguments passed through the
  34. ellipsis.  Note: There is no guaranteed method for accessing the arguments
  35. passed with the ... unless there is at least one argument before the
  36. ellipsis. 
  37.  
  38. > If there is a solution to that question (which I'm sure there is), is
  39. > there any way for me to know the number of arguments and, more
  40. > importantly, the variable type of each argument?
  41.  
  42. No.  You must either know this information a priori or pass it to the
  43. function. 
  44.  
  45. ===========================================================================
  46. Josh Mittleman (mittle@watson.ibm.com)
  47. J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY  10598
  48.