SnailTang
SnailTang

Reputation: 23

Passing a variable-length argument to a function without checking the length of it?

Could I pass a variable-length argument to a function without checking the length of it! That is Could I make a List or something others , and pass it to a variable-length argument function. I know we can us va_list to implement the function; But now, We get a argument list, and we need count the length, and then maybe we should define a number of variables, and pass them, Could we make it more convenient?

Upvotes: 1

Views: 160

Answers (1)

Jakub M.
Jakub M.

Reputation: 33827

You can always add a special "marker" argument at the end of the list indicating that it is finished, like NULL

char **args = { "one", "two", NULL }
function( args );
...
void function ( char **args ) {
  char *p;
  int i = 0;
  p = args[i];
  while( p != NULL) {
    ...
    i++;
    p = args[i];
  }
}

Upvotes: 1

Related Questions