home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: mittle@watson.ibm.com (Josh Mittleman)
- Subject: Re: Referencing `...' arguments in functions
- Sender: news@watson.ibm.com (NNTP News Poster)
- Message-ID: <1992Nov09.211314.162751@watson.ibm.com>
- Date: Mon, 09 Nov 1992 21:13:14 GMT
- Distribution: usa
- Disclaimer: This posting represents the poster's views, not necessarily those of IBM
- References: <b#n125j@rpi.edu>
- Nntp-Posting-Host: siena.watson.ibm.com
- Organization: IBM T.J. Watson Research Center
- Lines: 33
-
-
- > I wish to write a function having an unknown number of arguments. I know
- > that the '...' in the function declaration will allow for any number of
- > arguments, but how do I actually access these variables.
-
- According to ARM, p.146, the only guaranteed way to access the arguments
- passed using ... is the va_list macros in stdarg.h. They are used as
- follows:
-
- void f(int n, ...) {
- va_list theList;
- va_start(theList, n);
- for (int i = 0; i < n; i++)
- int j = va_arg(theList, int);
- cout << j << endl;
- va_end(theList);
- }
-
- This function prints out a list of the n int arguments passed through the
- ellipsis. Note: There is no guaranteed method for accessing the arguments
- passed with the ... unless there is at least one argument before the
- ellipsis.
-
- > If there is a solution to that question (which I'm sure there is), is
- > there any way for me to know the number of arguments and, more
- > importantly, the variable type of each argument?
-
- No. You must either know this information a priori or pass it to the
- function.
-
- ===========================================================================
- Josh Mittleman (mittle@watson.ibm.com)
- J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY 10598
-