C/C++: Exemple d’utilisation de liste d’argument ‘varArg’

Author:

 list, list
{filelink=14159}

#include 
#include 

double average( double valeur1, double valeur2,...)//liste illimitée
{
  va_list varArg;
  double somme = valeur1+valeur2;
  double valeur = 0;
  int count = 2;

  va_start(varArg,valeur2);//Initialisation de l'objet va_list

  while((valeur = va_arg(varArg, double)) != 0.0)
  {
    somme += valeur;
    count++;
  }
  va_end(varArg);
  printf("Somme des arguments: %dn",somme);
  return somme/count;
}

int main()
{

  printf("Moyenne = %dn", average(3.5, 4.5, 0.0));
  printf("Moyenne = %dn", average(1.0, 2.0));
  printf("Moyenne = %dn", average(0.0,1.2,1.5));

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *