算上C变量参数方法调用参数个数 [英] Count number of parameters in C variable argument method call

查看:137
本文介绍了算上C变量参数方法调用参数个数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的va_start(),在va_arg()和va_end用来()读取传递给方法的参数,是有办法统计有多少论点有?

据,如果你调用的va_arg()的次数太多,你得到随机误差的男子页:


  

如果没有一个参数,或者
  类型与类型兼容
  实际的下一个参数(如
  根据缺省促进
  参数提升),会发生随机错误。



解决方案

没有。变量参数函数(如的printf ),必须知道何时停止寻找更多的论据。

的printf 知悉%d个%S 和其他符号的格式字符串。

其它功能有时会使用哨兵值:

  sumValues​​(1,3,5,7,6,9,-1); //直到遇到一个将增加数-1

其他功能可以具有参数的数量表示前面

  AddNames(4,条例,爱丽丝,迈克,汤姆);

When using va_start(), va_arg() and va_end() to read parameters passed to a method, is there a way to count how many arguments there are?

According to the man page if you call va_arg() too many times you get "random errors":

If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), random errors will occur.

解决方案

No. a Variable Argument function (such as printf), must "know" when to stop looking for more arguments.

printf knows by the number of %d, %s and other symbols in its format string.

Other functions sometimes use Sentinel values:

sumValues(1, 3, 5, 7, 6, 9, -1); // will add numbers until it encounters a -1

Other functions may have the number of parameters stated up front:

AddNames(4, "Bill", "Alice", "Mike", "Tom");

这篇关于算上C变量参数方法调用参数个数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆