Variable Arguments <stdarg.h>

Top  Previous  Next

A function whose declaration contains an ellipsis '...' may be called with varying numbers of arguments. The facilities described here allow such a function to access its arguments.

 

The header <stdarg.h> defines a type va_list. Your function should declare an object of this type, called the argument pointer, and scan the variable-length argument list with it, using these functions.

 

Note that it is generally dangerous to attempt to scan over the arguments of a function by incrementing an ordinary pointer as in the following example:

 

void func(int a,...) {

   // this will not always work

   int b, *p = &a;

   b = *++p;

}

 

To maintain portability, you should use the stdarg.h functions instead.

 


va_start

initialize the argument pointer


va_arg

find the next argument


va_end

finish accessing arguments