Perhaps you would like to have a function that will accept any number of values and then return the average. You don't know how many arguments will be passed in to the function. One way you could make the function would be to accept a pointer to an array. Another way would be to write a function that can take any number of arguments. So you could write avg(4, 12.2, 23.3, 33.3, 12.1); or you could write avg(2, 2.3, 34.4); The advantage of this approach is that it's much easier to change the code if you want to change the number of arguments. Indeed, some library functions can accept a variable list of arguments (such as printf--I bet you've been wondering how that works!).
Whenever a function is declared to have an indeterminate number of arguments, in place of the last argument you should place an ellipsis (which looks like '...'), so, int a_function ( int x, ... ); would tell the compiler the function should accept however many arguments that the programmer uses, as long as it is equal to at least one, the one being the first, x.
We'll need to use some macros (which work much like functions, and you can treat them as such) from the stdarg.h header file to extract the values stored in the variable argument list -- va_start, which initializes the list, va_arg, which returns the next argument in the list, and va_end, which cleans up the variable argument list.
To use these functions, we need a variable capable of storing a variable-length argument list--this variable will be of type va_list. va_list is like any other type.
For example, the following code declares a list that can be used to store a variable number of arguments.
va_list a_list;
va_start is a macro which accepts two arguments, a va_list and the name of the variable that directly precedes the ellipsis ("..."). So in the function a_function, to initialize a_list with va_start, you would write va_start ( a_list, x );
int a_function ( int x, ... )
{
va_list a_list;
va_start( a_list, x );
}
To show how each of the parts works, take an example function:
#include <stdarg.h>
#include <stdio.h>
double average (int num, ... )
{
va_list arguments;
double sum = 0;
/* Initializing arguments to store all values after num */
va_start (arguments, num);
/* Sum all the inputs; we still rely on the function caller to tell us how
* many there are */
for (int x = 0; x < num; x++)
{
sum += va_arg (arguments, double);
}
va_end (arguments); // Cleans up the list
return sum / num;
}
int main()
{
/* this computes the average of 3 values */
printf( "%f\n", average (3, 12.2, 22.3, 4.5));
/* here it computes the average of the 5 values */
printf( "%f\n", average (5, 3.3, 2.2, 1.1, 5.5, 3.3));
}