va_arg(), va_start(), va_end() are used when you need to write a function that takes a variable number of arguments. You need to use the data type va_list so you can store the data items needed by va_start(), va_arg() and va_end().
example code, from the book that im reading.
Code:
#include <stdio.h>
#include <stdarg.h>
double Add(int x, ...);
int main()
{
double d1 =1.5;
double d2 =2.5;
double d3 =3.5;
double d4 = 4.5;
printf("Given an argument: %2.1f\n",d1);
printf("The result returned by Add() is: %2.1f\n\n",
Add(1,d1));
printf("Given arguments: %2.1f and %2.1f\n", d1,d2);
printf("The result returned by Add() is: %2.1f\n\n",
Add(2,d1,d2));
printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1,d2,d3);
printf("The result returned by Add() is %2.1f\n\n",
Add(3,d1,d2,d3));
printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n",
d1,d2,d3,d4);
printf("The result returned by Add() is: %2.1f\n",
Add(4,d1,d2,d3,d4));
return 0;
}
/*defintion of Add()*/
double Add(int x, ...)
{
va_list arglist;
int i;
double result =0;
printf("the number of arguments is: %d\n", x);
va_start(arglist, x);
for( i=0; i<x; i++)
result += va_arg(arglist,double);
va_end(arglist);
return result;
}
are you readin the same book that im reading? lol