C语言中可变函数参数变量的实现

VA_LIST:

VA_LIST 是在C语言中解决变参问题的一组宏,所在头文件:#include <stdarg.h>

他有这么几个成员

1) va_list型变量:

#ifdef _M_ALPHA
typedef struct {
char *a0; /* pointer to first homed integer argument */
int offset; /* byte offset of next parameter */
} va_list;
#else
typedef char * va_list;
#endif
_M_ALPHA是指DEC ALPHA(Alpha AXP)架构。所以一般情况下va_list所定义变量为字符[1]指针。

2)_INTSIZEOF 宏

获取类型占用的空间长度,最小占用长度为int的整数倍:
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )

3)VA_START宏

获取可变参数列表的第一个参数的地址(ap是类型为va_list的指针,v是可变参数最左边的参数):
#define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) )

4)VA_ARG宏

获取可变参数的当前参数,返回指定类型并将指针指向下一参数(t参数描述了当前参数的类型):
#define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )

5)VA_END宏

清空va_list可变参数列表:
#define va_end(ap) ( ap = (va_list)0 )
 

VA_LIST的用法:

1)首先在函数里定义一具VA_LIST型的变量,这个变量是指向参数的指针;
(2)然后用VA_START宏初始化变量刚定义的VA_LIST变量;
(3)然后用VA_ARG返回可变的参数,VA_ARG的第二个参数是你要返回的参数的类型(如果函数有多个可变参数的,依次调用VA_ARG获取各个参数);
(4)最后用VA_END宏结束可变参数的获取。

应该注意的问题:

(1)可变参数的类型和个数完全由程序代码控制,它并不能智能地识别不同参数的个数和类型;
(2)如果我们不需要一一详解每个参数,只需要将可变列表拷贝至某个缓冲,可用vsprintf函数;
(3)因为编译器对可变参数的函数的原型检查不够严格,对编程查错不利.不利于我们写出高质量的代码;
 
一个简易的功能实现如下:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <stack>

int show( int value,...)
{
    int argCnt = 0;
    //-1作为输出的结束符,毕竟是简易的嘛! 
    int ret;
    int buf[256];
    va_list ap;
    va_start(ap,value);
    
    printf("%d\n",value);
    ret = va_arg(ap,int);
    while( ret != -1 )
    {
        argCnt++;
        printf("%d\n",ret);
        ret = va_arg(ap,int);
    }
    
    return argCnt;
    
}

int main()
{
    show(21,31,55,32,-1);
    return 0;
}

一个很不错的printf的的功能实现如下:

View Code
  1 #include <stdio.h>
  2 #include <stdarg.h>
  3 #include "print.h"
  4 
  5 int main(void)
  6 {
  7     print("print: %c\n", 'c');
  8     print("print %d\n", 1234567);
  9     print("print: %f\n", 1234567.1234567);
 10     print("print: %s\n", "string test");
 11     print("print: %b\n", 0x12345ff);
 12     print("print: %x\n", 0xabcdef);
 13     print("print: %%\n");
 14     return 0;
 15 }
 16 
 17 void    print(char* fmt, ...)
 18 {
 19     double vargflt = 0;
 20     int  vargint = 0;
 21     char* vargpch = NULL;
 22     char vargch = 0;
 23     char* pfmt = NULL;
 24     va_list vp;
 25 
 26     va_start(vp, fmt);
 27     pfmt = fmt;
 28 
 29     while(*pfmt)
 30     {
 31         if(*pfmt == '%')
 32         {
 33             switch(*(++pfmt))
 34             {
 35                 
 36                 case 'c':
 37                     vargch = va_arg(vp, int); 
 38                     /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
 39                         mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
 40                     printch(vargch);
 41                     break;
 42                 case 'd':
 43                 case 'i':
 44                     vargint = va_arg(vp, int);
 45                     printdec(vargint);
 46                     break;
 47                 case 'f':
 48                     vargflt = va_arg(vp, double);
 49                     /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
 50                         mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
 51                     printflt(vargflt);
 52                     break;
 53                 case 's':
 54                     vargpch = va_arg(vp, char*);
 55                     printstr(vargpch);
 56                     break;
 57                 case 'b':
 58                 case 'B':
 59                     vargint = va_arg(vp, int);
 60                     printbin(vargint);
 61                     break;
 62                 case 'x':
 63                 case 'X':
 64                     vargint = va_arg(vp, int);
 65                     printhex(vargint);
 66                     break;
 67                 case '%':
 68                     printch('%');
 69                     break;
 70                 default:
 71                     break;
 72             }
 73             pfmt++;
 74         }
 75         else
 76         {
 77             printch(*pfmt++);
 78         }
 79     }
 80     va_end(vp);
 81 }
 82 
 83 void    printch(char ch)
 84 {
 85     console_print(ch);
 86 }
 87 
 88 void    printdec(int dec)
 89 {
 90     if(dec==0)
 91     {
 92         return;
 93     }
 94     printdec(dec/10);
 95     printch( (char)(dec%10 + '0'));
 96 }
 97 
 98 void    printflt(double flt)
 99 {
100     int icnt = 0;
101     int tmpint = 0;
102     
103     tmpint = (int)flt;
104     printdec(tmpint);
105     printch('.');
106     flt = flt - tmpint;
107     tmpint = (int)(flt * 1000000);
108     printdec(tmpint);
109 }
110 
111 void    printstr(char* str)
112 {
113     while(*str)
114     {
115         printch(*str++);
116     }
117 }
118 
119 void    printbin(int bin)
120 {
121     if(bin == 0)
122     {
123         printstr("0b");
124         return;
125     }
126     printbin(bin/2);
127     printch( (char)(bin%2 + '0'));
128 }
129 
130 void    printhex(int hex)
131 {
132     if(hex==0)
133     {
134         printstr("0x");
135         return;
136     }
137     printhex(hex/16);
138     if(hex < 10)
139     {
140         printch((char)(hex%16 + '0'));
141     }
142     else
143     {
144         printch((char)(hex%16 - 10 + 'a' ));
145     }
146 }

C函数库中的printf()的函数实现如下:

int printf(const char *fmt, ...)
  {
   int i;
   char buf[256];
  
   va_list arg = (va_list)((char*)(&fmt) + 4); 
   i = vsprintf(buf, fmt, arg);
   write(buf, i);
  
   return i;
  }

参考资料:

http://baike.baidu.com/view/1213054.htm

http://blog.csdn.net/xfeng88/article/details/6695848

原文地址:https://www.cnblogs.com/wickedboy237/p/3022978.html