[zz]va_start() 和 va_end()函数应用

man:
       #include <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);


1:当无法列出传递函数的所有实参的类型和数目时,可用省略号指定参数表
void foo(...);
void foo(parm_list,...);

2:函数参数的传递原理
函数参数是以数据结构:栈的形式存取,从右至左入栈.
eg:
#include   
void fun(int a, ...)
{
    int *temp = &a;
    temp++;
    for (int i = 0; i < a; ++i)
    {
        cout << *temp << endl;
        temp++;
    }
}


int main()
{
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    fun(4, a, b, c, d);
    system("pause");
    return 0;
}


Output::
1
2
3
4

3:获取省略号指定的参数
在函数体中声明一个va_list,然后用va_start函数来获取参数列表中的参数,使用完毕后调用va_end()结束。

4.va_start使argp指向第一个可选参数。va_arg返回参数列表中的当前参数并使argp指向参数列表中的下一个参数。va_end把argp指针清为NULL。函数体内可以多次遍历这些参数,但是都必须以va_start开始,并以va_end结尾。

实例:
编写vstart.c,如下:

 

[c-sharp] view plaincopy
  1. //vstart.c  
  2. #include <stdio.h>  
  3. #include <strings.h>  
  4. #include <stdarg.h>  
  5. int demo(char *fmt, ...);  
  6. int main()   
  7. {   
  8.    demo("DEMO""This""is""a""demo!""");   
  9.    return 0;  
  10. }   
  11. int demo( char *fmt, ... )   
  12. {   
  13.    va_list argp;   
  14.    int argno = 0;    
  15.    char *para;   
  16.    va_start(argp, fmt);   
  17.    while (1)   
  18.    {   
  19.       para = va_arg(argp, char *);   
  20.       if (strcmp( para, "") == 0)   
  21.       break;   
  22.       printf("Parameter #%d is: %s/n", argno, para);   
  23.       argno++;   
  24.    }   
  25.    va_end( argp );   
  26.    return 0;   
  27. }   

 

 

编译运行:

[root@fly test]# gcc -o vstart vstart.c   
[root@fly test]# ./vstart 
Parameter #0 is: This
Parameter #1 is: is
Parameter #2 is: a
Parameter #3 is: demo!
[root@fly test]#

 

注意:va_arg()的格式:

type va_arg(va_list ap, type);

因此:

int d;
char c, *s;

d = va_arg(ap, int);   /* int */

c = (char) va_arg(ap, int);  /* char */

s = va_arg(ap, char *);   /* string */

 

 

实例2:(start.c)

 

[c-sharp] view plaincopy
  1. #include <stdio.h>  
  2. #include <stdarg.h>  
  3. #include <string.h>  
  4. int foo(char *fmt, ...);  
  5. int main()  
  6. {  
  7.   char *a = "ast";  
  8.   int b = 224;  
  9.   char c = 'x';  
  10.    
  11.   foo("%s,%d,%c/n",a,b,c);  
  12.   return 0;  
  13. }  
  14. int foo(char *fmt, ...)  
  15. {  
  16.   va_list ap;  
  17.   int d;  
  18.   char c, *s;  
  19.   va_start(ap, fmt);  
  20.   while (*fmt)  
  21.   switch(*fmt++)  
  22.   {  
  23.      case 's':           /* string */  
  24.         s = va_arg(ap, char *);  
  25.         printf("string %s/n", s);  
  26.         break;  
  27.      case 'd':           /* int */  
  28.         d = va_arg(ap, int);  
  29.         printf("int %d/n", d);  
  30.         break;  
  31.      case 'c':           /* char */  
  32.                              /* need a cast here since va_arg only 
  33.                                 takes fully promoted types */  
  34.         c = (char) va_arg(ap, int);  
  35.         printf("char %c/n", c);  
  36.         break;  
  37.   }  
  38.   va_end(ap);  
  39.   return 0;  
  40. }  
  41.                 

 

 

编译运行:

[root@fly test]# gcc -o start start.c    
[root@fly test]# ./start              
string ast
int 224
char x
[root@fly test]#

注意foo()格式:

  foo("%s,%d,%c/n",a,b,c);

posted @ 2013-01-06 10:48  zaleilynn  阅读(262)  评论(0编辑  收藏  举报