可变参数使用
在C中,可变参数用于参数个数,类型不确定的情况,如printf,snprintf函数的实现。
当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表
void func(...); void func(parm_list,...);这是C传参的一种形式,与固定参数不同。
函数参数的传递原理
函数参数以栈的形式存储,从右往左入栈。
举个例子:
在调用函数的时候,实参z先入栈,然后是y,最后是x。在内存中变量的存放次序是x->y->z。所以,从理论上来说,只要知道x,y,z其中一个变量的地址和类型,通过指针运算,可找到其他变量。
va_list是指向当前参数的指针,通过这个指针进行取参。
宏的使用方式如下:
例子:
#include <stdio.h> #include <stdlib.h> #include <stdarg.h>int my_snprintf(char *dst, int size, char *fmt, ...) {int len;va_list argp;va_start(argp, fmt);len = vsnprintf(dst, size, fmt, argp);len = len > size - 1 ? size - 1 : len;va_end(argp);return len; }int main(void) {char str[8];int len;len = my_snprintf(str, sizeof(str), "A:%d:%s", 1, "ABCDEFGH");printf("str:%s, len:%d\n", str, len);return 0; }总结
- 上一篇: 零元学Expression Blend
- 下一篇: list.h在用户态下的应用