varargs.cpp

来自「Since the field of object oriented progr」· C++ 代码 · 共 44 行

CPP
44
字号
                               // Chapter 4 - Program 5 - VARARGS.CPP
#include <iostream.h>
#include <stdarg.h>

            // Declare a function with one required parameter
void display_var(int number, ...);

int main()
{
int index = 5;
int one = 1, two = 2;

   display_var(one, index);
   display_var(3, index, index + two, index + one);
   display_var(two, 7, 3);

   return 0;
}


void display_var(int number, ...)
{
va_list param_pt;

   va_start(param_pt, number);              // Call the setup macro

   cout << "The parameters are ";
   for (int index = 0 ; index < number ; index++)
   { 
      cout << va_arg(param_pt, int) << " "; // Extract a parameter
   }
   cout << "\n";
   va_end(param_pt);                        // Closing macro
}




// Result of Execution
//
// The parameters are 5
// The parameters are 5 7 6
// The parameters are 7 3

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?