Answers for "ellipsis c lang"

C
0

ellipsis c lang

#include <stdarg.h>

// The ellipsis must be the last parameter
// count is how many additional arguments we're passing
double findAverage(int count, ...) {
  	double sum = 0;
	// We access the ellipsis through a va_list
	va_list list;
  
    // Initializing list
  	// The first parameter is list to initialize
    // The second parameter is the last non-ellipsis param
    va_start(list, count);
 
    // Loop through all the ellipsis arguments
  	int i;
    for (i = 0; i < count; ++i)
    {
         // We use va_arg to get parameters out of our ellipsis
         // The first parameter is the va_list we're using
         // The second parameter is the type of the parameter
         sum += va_arg(list, int);
    }
    // Cleanup the va_list when we're done.
    va_end(list);
  	return sum / count;
}
Posted by: Guest on June-19-2021

Code answers related to "C"

Browse Popular Code Answers by Language