开发者

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!开发者_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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜