ellipsis c lang

Solutions on MaxInterview for ellipsis c lang by the best coders in the world

showing results for - "ellipsis c lang"
Edoardo
14 Jun 2020
1#include <stdarg.h>
2
3// The ellipsis must be the last parameter
4// count is how many additional arguments we're passing
5double findAverage(int count, ...) {
6  	double sum = 0;
7	// We access the ellipsis through a va_list
8	va_list list;
9  
10    // Initializing list
11  	// The first parameter is list to initialize
12    // The second parameter is the last non-ellipsis param
13    va_start(list, count);
14 
15    // Loop through all the ellipsis arguments
16  	int i;
17    for (i = 0; i < count; ++i)
18    {
19         // We use va_arg to get parameters out of our ellipsis
20         // The first parameter is the va_list we're using
21         // The second parameter is the type of the parameter
22         sum += va_arg(list, int);
23    }
24    // Cleanup the va_list when we're done.
25    va_end(list);
26  	return sum / count;
27}