variable-length argument lists
来源:互联网 发布:react php 编辑:程序博客网 时间:2024/06/05 15:16
The best example of variable-length argument lists function is printf()
the proper declaration for printf() is
int printf(char * format,...)
where the declaration ... means that the number and types of these arguments may vary.the declaration ... can only appear at the end of an argument list, ourmainprintfis declared as
int minprintf(char * fmt,...)
since we will not return the character count the the printf does.
the tricky bit is how minprintf walks along the argument list when the list doesn't even have a name. the standard header<stdarg.h> contains a set of macro definitions that define how to step through an argument list.the implementation of this header will vary from machine to machine,but the interface it presents is uniform.
the type va_list is used to declare a variable that will refer to each argument in turn; in minprintf ,this variable is call ap.for "argument pointer" the macro va_start initialize ap to point to the first unnamed argument.it must be called once beforeapis used.there must be at least one named argument;the final argument is used by va_start to get started.each call ofva_argreturns one argument and steps ap to the next; va_arg uses a type name to determine what type to return and how big a step to take.finally ,va_end does whatever cleanup is necessary.it must be called before the function returns.
the properties form the basis of our simplified printf:
[code=C/C++]#include<stdarg.h>void minprintf(char * fmt,...) { va_list ap; char * p,*sval; int ival; double dval; va_start(ap,fmt);//make ap point to first unnamed arg for(p=fmt;*p;p++) { if(*p != '%') { putchar(*p); continue; } switch(*++p) { case 'd': ival = va_arg(ap,int); printf("%d",ival); break; case 'f': dval = va_arg(ap,double); printf("%f",dval); break; case 's': for(sval = va_arg(ap,char *);*sval;sval++) { putchar(*sval); } break; default: putchar(*p); break; } } va_end(ap);//clean up when done }[/code]
- variable-length argument lists
- Variable argument lists in Cocoa
- C#游长变元表(variable-length argument list)
- C语言中的 stdarg - variable argument lists
- Learning Perl: 4.6. Variable-Length Parameter Lists
- stdarg(3) variable argument lists 可变参数列表
- variable argument
- n个数字连乘的计算(Variable-Length Argument List)
- variable argument list
- pass variable-argument list
- variable argument print function
- 可变参数(variable-argument)
- 14.4.1 Argument lists
- Unpacking Argument Lists
- variable length arrays
- variable argument parameter的区别
- 函数参数Variable-length Arguments
- Methods with Empty/One Argument Lists
- 寻找最近点对
- final static
- Nagios安装、配置、问题记录
- 一个fork引起的“穿越”事件。
- 今天星期五
- variable-length argument lists
- 0706
- 原生JS可拖动弹窗效果
- 指针与字符串的秘密&赋值运算返回值&printf()返回值
- Java使用基本JDK操作ZIP文件
- 在ubuntu上安装wine以及QQ2012
- magento 产品评论模块的显示
- 0702
- 0704