va_list用法

怎样写出一个可以处理想printf一样能够处理可变长参数的函数呢。

看printf的定义:

int printf(char *fmt, ...);

C语言标准库中头文件stdarg.h索引的接口包含了一组能够遍历变长参数列表的宏。主要包含下面几个:

1、va_list 用来声明一个表示参数表中各个参数的变量。

2、va_start 初始化一个指针来指向变长参数列表的头一个变量(注意,...只能出现在参数表的最后)

3、va_arg每次调用时都会返回当前指针指向的变量,并将指针挪至下一个位置,参数的类型需要在这个调用的第二个参数来指定,va_arg也是根据这个参数来判断偏移的距离。

4、va_end需要在函数最后调用,来进行一些清理工作。

下面上一个简单的例子。

复制代码
 1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <string.h>
4
5
6 void write_log( char *fmt, ...)
7 {
8 va_list va;
9 char buf[1024];
10
11 va_start(va, fmt);
12 memset(buf, 0, 1024);
13 (void) vsprintf(buf, fmt, va);
14 va_end(va);
15
16 printf("%s-%s", "my_log_prehead", buf);
17 }
18
19
20 void read_num(int num, ...)
21 {
22 va_list va; /*point to each unamed variables in arg list*/
23 va_start(va, num); /*start va_list from num, and va goes to the second one, and this is the first vary variable*/
24 while(num--)
25 {
26 printf("%d\t", va_arg(va, int)); /*get a arg, va goes to the next*/
27 }
28 va_end(va); /*end the va*/
29 }
30
31 int main()
32 {
33 write_log("%s\n", "hello world!");
34 read_num(3, 111, 222, 333);
35 return 0;
36 }

结果输出:

[wren@buster c_prime]$ ./read_num
my_log_prehead-hello world!
111 222 333

vsprintf中va_list的用法也是经常会产生误会的地方,留在这里,以供参考。

原文地址:https://www.cnblogs.com/rooney/p/2604021.html