home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX0415.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  1002 b   |  29 lines

  1. // \EXAMPLES\EX0415.CPP
  2. #include <iostream.h>
  3. #include <stdarg.h>      // defines va_start, va_end, and va_end
  4.  
  5. void f(int a, int num_args,...) {
  6.    va_list arg_pointer; //  pointer to the list of arguments
  7.    va_start(arg_pointer, num_args);
  8.                         // va_start makes its first argument
  9.                         // point to the unspecified arguments
  10.    cout << "Here is argument a: " << a << endl;
  11.    for ( int i = 0; i < num_args; i++) {
  12.       cout << "Here is the unspecified argument " << i + 1;
  13.       cout << ": " << va_arg(arg_pointer, int) << endl;
  14.          // va_arg takes a pointer to unspecified arguments and a
  15.          // type name as arguments.  It returns an unspecified
  16.          // argument of the types.
  17.    }
  18.    va_end(arg_pointer);
  19. }
  20.  
  21. void main() {
  22.    int a = 0;
  23.    int b = 1;
  24.    int c = 2;
  25.    f(a, 0);       // the second argument of f() is the number of
  26.    f(a, 1, b);    // unspecified arguments that are being passed.
  27.    f(a, 2, b, c);
  28. }
  29.