打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
va_arg
userphoto

2011.11.07

关注
macro
<cstdarg>
type va_arg ( va_list ap, type )
Retrieve next argument
This macro expands to an expression that has the type type and the value of the next argument in the variable arguments list.
The next call to this macro will expand to the following argument in the same order as passed to the function.
Notice that va_arg cannot determine the actual type of the argument passed to the function, but uses whatever type is passed as the type macro argument as its type.
Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the amount of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read.
Parameters
ap
Object of type va_list with information about the additional arguments an their retrieval state. This object shall have been initialized by an initial call to va_start before the first call to va_arg.
type
A type name. This type name is used as the type of the expression this macro expands to (i.e., its return type).
For a type expression to be suitable for its use with va_arg it must be such that when an asterisk (*) would be appended to its right the resulting expression would be a valid pointer type to a type object.
Return Value
Returns the next additional argument as an expression of type type.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* va_arg example */#include <stdio.h>#include <stdarg.h>int FindMax ( int amount, ...){ int i,val,greater; va_list vl; va_start(vl,amount); greater=va_arg(vl,int); for (i=1;i<amount;i++) { val=va_arg(vl,int); greater=(greater>val)?greater:val; } va_end(vl); return greater;}int main (){ int m; m= FindMax (7,702,422,631,834,892,104,772); printf ("The greatest one is: %d\n",m); return 0;}
This FindMax function takes as its first parameter the amount of additional arguments it is going to get. The first additional argument is retrieved and used as an initial reference for comparison, and then the remaining additional arguments are retrieved in a loop and compared to return the greatest one (which in this case is 892).
See also
va_startInitialize a variable argument list (macro)
va_endEnd using variable argument list (macro)
va_listType to hold information about variable arguments (type)
vsprintfPrint formatted variable argument list to string (function)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Variable Argument Lists in C
深入 了解 C宏
奥瑞那设计手册第一部分火灾报警及消防联动控制系统产品设计应用手册一
Function Objects (STL)
如何写参数个数不定的函数(即类似printf的函数)
变参函数的实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服