Pointers on C——7 Functions.9

7.6.2 Limitations of Variable Arguments

Note that the variable arguments must be accessed one by one, in order, from start to finish. You can stop early if you wish, but there isnʹt any way to begin accessing in the middle of the list. In addition, because the variable portion of the argument list is not prototyped, the default argument promotions are performed on all values passed as variable arguments.

注意,可变参数必须从头到尾按照顺序逐个访问。如果你在访问了几个可变参数后想半途中止,这是可以的。但是,如果你想一开始就访问参数列表中间的参数,那是不行的。另外,由于参数列表中的可变参数部分并没有原型,所以,所有作为可变参数传递给函数的值都将执行缺省参数类型提升。


You may also have noticed the requirement that there must be at least one named argument in the argument list; you cannot use va_start without it. This argument provides a way to locate the varying portion of the argument list.

你可能同时注意到参数列表中至少要有一个命名参数。如果连 个命名参数也没有,你就无法使用va_start 。这个参数提供了一种方法,用于查找参数列表的可变部分。


There are two fundamental limitations of these macros, both of which are a direct result of the fact that the type of a value cannot be determined simply by examining it.

对于这些宏,存在两个基本的限制。一个值的类型无法简单地通过检查它的位模式来判断,这两个限制就是这个事实的直接结果。


1. The macros cannot determine how many arguments actually exist.

1.这些宏无法判断实际存在的参数的数量。


2. The macros cannot determine the type of each argument.

2. 这些宏无法判断每个参数的类型。


The named argument(s) must answer both of these questions. In Program 7.9b, the named argument specifies how many values were passed, but their types are assumed to be integers. The named argument in printf is the format string, which indicates both the number and types of the arguments.

要回答这两个问题,就必须使用命名参数。在程序7.怖中,命名参数指定了实际传递的参数数量,不过它们的类型被假定为整型。printf 函数中的命名参数是格式字符串,它不仅指定了参数的数量,而且指定了参数的类型。


If you specify the wrong type with va_arg the results are unpredictable. This error is easy to make because va_arg doesnʹt compensate for the default argument promotions that are performed on variable arguments, char, short, and float values will actually be passed as intʹs and doubleʹs. Be careful to use these latter types with va_arg.

如果你在va_arg 中指定了错误的类型,那么其结果是不可预测的。这个错误是很容易发生的,因为va_arg 无法正确识别作用于可变参数之上的缺省参数类型提升。char、short 和float 类型的值实际上将作为int 或double 类型的值传递给函数。所以你在va_arg 中使用后面这些类型时应该小心。

上一章 Pointers on C——7 Functions.8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值