可变参数函数

来源:互联网 发布:独立域名邮箱 编辑:程序博客网 时间:2024/06/05 16:31

本文转自:http://www.jb51.net/article/43192.htm,http://blog.csdn.net/weiwangchao_/article/details/4857567

先看代码

printf(“hello,world!”);其参数个数为1个。printf(“a=%d,b=%s,c=%c”,a,b,c);其参数个数为4个。

如何编写可变参数函数呢?我们首先来看看printf函数原型是如何定义的。
在linux下,输入man 3 printf,可以看到prinf函数原型如下:

SYNOPSIS#include <stdio.h>int printf(const char *format, ...);

后面的三个点...表示printf参数个数是不定的.
如何实现可变参数函数?
2. 编写可变函数准备
为了编写可变参数函数,我们通常需要用到<stdarg.h>头文件下定义的以下函数:

void va_start(va_list ap, last);type va_arg(va_list ap, type);void va_end(va_list ap);void va_copy(va_list dest, va_list src);

其中:
va_list是用于存放参数列表的数据结构。
va_start函数根据初始化last来初始化参数列表。
va_arg函数用于从参数列表中取出一个参数,参数类型由type指定。
va_copy函数用于复制参数列表。
va_end函数执行清理参数列表的工作。
上述函数通常用宏来实现,例如标准ANSI形式下,这些宏的定义是:

typedef char * va_list; //字符串指针#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )#define va_end(ap) ( ap = (va_list)0 )

使用宏_INTSIZEOF是为了按照整数字节对齐指针,因为c调用协议下面,参数入栈都是整数字节(指针或者值)。
函数官方说明,如果你看到英文就烦,可以自行忽略以下说明。
va_start()
       The  va_start() macro initializes ap for subsequent use by va_arg() and
       va_end(), and must be called first.
       The argument last is the name of the last argument before the  variable
       argument list, that is, the last argument of which the calling function
       knows the type.
       Because the address of this argument may  be  used  in  the  va_start()
       macro,  it should not be declared as a register variable, or as a func‐
       tion or an array type.
va_arg()
       The va_arg() macro expands to an expression that has the type and value
       of  the  next  argument in the call.  The argument ap is the va_list ap
       initialized by va_start().  Each call to va_arg() modifies ap  so  that
       the  next  call returns the next argument.  The argument type is a type
       name specified so that the type of a pointer to an object that has  the
       specified type can be obtained simply by adding a * to type.
       The  first use of the va_arg() macro after that of the va_start() macro
       returns the argument after last.   Successive  invocations  return  the
       values of the remaining arguments.
       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.
       If  ap is passed to a function that uses va_arg(ap,type) then the value
       of ap is undefined after the return of that function.
va_end()
       Each invocation of va_start() must be matched by a corresponding  invo‐
       cation of va_end() in the same function.  After the call va_end(ap) the
       variable ap is undefined.  Multiple traversals of the list, each brack‐
       eted  by va_start() and va_end() are possible.  va_end() may be a macro
       or a function.

GNU给出的一个实例:

#include <stdio.h>#include <stdarg.h>voidfoo(char *fmt, ...){  va_list ap;  int d;  char c, *s; va_start(ap, fmt); while (*fmt)     switch (*fmt++) {     case 's': /* string */     s = va_arg(ap, char *);         printf("string %s\n", s);         break;     case 'd': /* int */         d = va_arg(ap, int);         printf("int %d\n", d);         break;     case 'c': /* char *//* need a cast here since va_arg only takes fully promoted types */        c = (char) va_arg(ap, int);        printf("char %c\n", c);        break;   }   va_end(ap);}

说明:
va_start(ap, fmt);用于根据fmt初始化可变参数列表。
va_arg(ap, char *);用于从参数列表中取出一个参数,其中的char *用于指定所取的参数的类型为字符串。每次调用va_arg后,参数列表ap都会被更改,以使得下次调用时能得到下一个参数。
va_end(ap);用于对参数列表进行一些清理工作。调用完va_end后,ap便不再有效。
以上程序给了我们一个实现printf函数的是思路,即:通过调用va_start函数,来得到参数列表,然后我们一个个取出参数来进行输出即可。
3.实例
例如:对于printf(“a=%d,b=%s,c=%c”,a,b,c)语句;fmt的值为a=%d,b=%s,c=%c,调用va_start函数将参数a,b,c存入了ap中。注意到:fmt中的%为特殊字符,紧跟%后的参数指明了参数类型.
因此我们的简易printf函数如下:

#include <stdio.h>#include <stdarg.h>voidmyprintf(char *fmt, ...){  va_list ap;  int d;  double f;  char c;   char *s;  char flag;  va_start(ap,fmt);  while (*fmt){   flag=*fmt++;   if(flag!='%'){ putchar(flag); continue;  }  flag=*fmt++;//记得后移一位    switch (flag)   {    case 's': s=va_arg(ap,char*); printf("%s",s); break;   case 'd': /* int */          d = va_arg(ap, int);          printf("%d", d);          break;        case 'f': /* double*/          d = va_arg(ap,double);          printf("%d", d);          break;   case 'c': /* char*/    c = (char)va_arg(ap,int);         printf("%c", c);         break;   default: putchar(flag); break;  }     }  va_end(ap);}int main(){  char str[10]="linuxcode";  int i=1024;  double f=3.1415926;  char c='V';  myprintf("string is:%s,int is:%d,double is:%f,char is :%c",str,i,f,c);}

从上面我们可以知道可变参数函数的编写,必须要传入一个参数fmt,用来告诉我们的函数怎样去确定参数的个数。我们的可变参数函数是通过自己解析这个参数来确定函数参数个数的。
比如,我们编写一个求和函数,其函数实现如下:

int sum(int cnt,...){    int sum=0;int i;    va_list ap;    va_start(ap,cnt);for(i=0;i<cnt;++i) sum+=va_arg(ap,int);    va_end(ap);return sum;}

总结一下就是:通过va_start初始化参数列表(也就能得到具体的参数个数了),然后使用va_arg函数从参数列表中取出你想要的参数,最后调用va_end执行清理工作。

二、可变参类型陷阱

下面的代码是错误的,运行时得不到预期的结果:

view plaincopy to clipboardprint?
va_start(pArg, plotNo);   
fValue = va_arg(pArg, float);  // 类型应改为double,不支持float   
va_end(pArg);  
va_start(pArg, plotNo);
fValue = va_arg(pArg, float);  // 类型应改为double,不支持float
va_end(pArg);

下面列出va_arg(argp, type)宏中不支持的type:

—— char、signed char、unsigned char
—— short、unsigned short
—— signed short、short int、signed short int、unsigned short int
—— float

在C语言中,调用一个不带原型声明的函数时,调用者会对每个参数执行“默认实际参数提升(default argument promotions)”。该规则同样适用于可变参数函数——对可变长参数列表超出最后一个有类型声明的形式参数之后的每一个实际参数,也将执行上述提升工作。

提升工作如下:
——float类型的实际参数将提升到double
——char、short和相应的signed、unsigned类型的实际参数提升到int
——如果int不能存储原值,则提升到unsigned int

然后,调用者将提升后的参数传递给被调用者。

所以,可变参函数内是绝对无法接收到上述类型的实际参数的。


关于该陷井,C/C++著作中有以下描述:


在《C语言程序设计》对可变长参数列表的相关章节中,并没有提到这个陷阱。但是有提到默认实际参数提升的规则:
在没有函数原型的情况下,char与short类型都将被转换为int类型,float类型将被转换为double类型。
                ——《C语言程序设计》第2版  2.7 类型转换 p36

在其他一些书籍中,也有提到这个规则:

事情很清楚,如果一个参数没有声明,编译器就没有信息去对它执行标准的类型检查和转换。
在这种情况下,一个char或short将作为int传递,float将作为double传递。
这些做未必是程序员所期望的。
脚注:这些都是由C语言继承来的标准提升。
对于由省略号表示的参数,其实际参数在传递之前总执行这些提升(如果它们属于需要提升的类型),将提升后的值传递给有关的函数。——译者注
                ——《C++程序设计语言》第3版-特别版 7.6 p138

…… float类型的参数会自动转换为double类型,short或char类型的参数会自动转换为int类型 ……
                ——《C陷阱与缺陷》 4.4 形参、实参与返回值 p73

这里有一个陷阱需要避免:
va_arg宏的第2个参数不能被指定为char、short或者float类型。
因为char和short类型的参数会被转换为int类型,而float类型的参数会被转换为double类型 ……
例如,这样写肯定是不对的:
c = va_arg(ap,char);
因为我们无法传递一个char类型参数,如果传递了,它将会被自动转化为int类型。上面的式子应该写成:
c = va_arg(ap,int);
                ——《C陷阱与缺陷》p164


0 0