开发者

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

开发者 https://www.devze.com 2023-03-29 09:15 出处:网络
Could I pass a variable-length argument to a function without checking the length of it!开发者_JAVA技巧

Could I pass a variable-length argument to a function without checking the length of it!开发者_JAVA技巧 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?


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];
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消