James asked:
> how do i create a function that has an undetermined number of
> parameters?
>
Here's a small sample:
#include <stdio.h>
#include <stdarg.h>
void variadic_func( char *arg1, ... )
{
va_list list;
char *arg;
if( arg1 != NULL ){
printf("%s ", arg1);
va_start( list, arg1 );
while( NULL != ( arg = va_arg( list, char * ) ) ){
printf("%s ");
}
puts("");
va_end( list );
}
}
int main( int argc, char *argv[] )
{
variadic_func("foo",NULL);
variadic_func("foo","baz","bar",NULL);
return( 0 );
}
You need stdarg.h for the macro/type definitions. Your function
nees to have at least one real parameter before the variable
length parameter list. You use this to initialize the va_list type
pointer to the argument list using va_start. Then you grap the
parameters using va_arg giving the list pointer and the type.
Please note that you need to know type and number of
arguments you pass - if you try to fetch more than there is,
you'll get a SEGV. In my example, I stick to char * and the
final NULL to terminate the argument list. If this is unclear,
chekc either K&R or the stdarg(3) manpage.
HTH,
Thomas