var args has to be the last one : function parameters « Function « C++ Tutorial






#include <cstdio>
#include <cstdarg>
#include <iostream>
using namespace std;

bool debug = false;

void debugOut(char* str, ...)
{
  va_list ap;
  if (debug) {
    va_start(ap, str);
    vfprintf(stderr, str, ap);
    va_end(ap);
  }
}

void printInts(int num, ...)
{
  int temp;
  va_list ap;
  va_start(ap, num);
  for (int i = 0; i < num; i++) {
    temp = va_arg(ap, int);
    cout << temp << " ";
  }
  va_end(ap);
  cout << endl;
}

int main(int argc, char** argv)
{
  debug = true;
  debugOut("int %d\n", 5);
  debugOut("String %s and int %d\n", "hello", 5);
  debugOut("Many ints: %d, %d, %d, %d, %d\n", 1, 2, 3, 4, 5);

  printInts(5, 5, 4, 3, 2, 1);

  return (0);
}








7.3.function parameters
7.3.1.Passing int by value
7.3.2.Define function to accept three int parameters
7.3.3.Pass a pointer to a function.
7.3.4.Pass variable address to a function
7.3.5.Pass int array to a function
7.3.6.Declare int array parameter for a function without indicating the array length
7.3.7.Function parameter: Use int pointer to accept an array
7.3.8.Use array as function's parameter
7.3.9.Change the contents of an array using a function
7.3.10.Pass a string to a function: Invert the case of the letters within a string
7.3.11.Change a call-by-value parameter does not affect the argument
7.3.12.Demonstrate the pointer version of swap(): Exchange the values of the variables pointed to by x and y
7.3.13.Using reference parameters
7.3.14.Passing a two-dimensional array to a function
7.3.15.Passing an array to a function
7.3.16.var args has to be the last one
7.3.17.the use of ... and its support macros va_arg, va_start, and va_end
7.3.18.Handling an array parameter as a pointer