大宇私人领地伊甸园丨宁愿做了后悔,也不要错过后悔[www.mrdayu.com]
注册

24小时联系邮箱:[email protected]

所有文章
大宇私人伊甸园: 首页 > 所有文章 > 编程人生 > C/C++语言 > C标准库详解 > va_list详解


友情链接:


va_list详解

发布时间:2018/12/07 C标准库详解 标签va_list阅读:20

官方介绍:

va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end.

If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.

It is legal to pass a pointer to a va_list object to another function and then use that object after the function returns.

va_list历史:

C11 standard (ISO/IEC 9899:2011):    7.16/3 Variable arguments <stdarg.h> (p: 269)
C99 standard (ISO/IEC 9899:1999):    7.15/3 Variable arguments <stdarg.h> (p: 249)
C89/C90 standard (ISO/IEC 9899:1990):    4.8 VARIABLE ARGUMENTS <stdarg.h>

扩展:

va_arg
accesses the next variadic function argument (function macro)
va_copy(C99)
makes a copy of the variadic function arguments (function macro)
va_end
ends traversal of the variadic function arguments (function macro)
va_start
enables access to variadic function arguments

Linux平台测试:

#include <stdarg.h>
#include <stdio.h>
int myprintf(const int num1,...){
        va_list argptr;
        va_start(argptr,num1);
        int ch;
        while(ch=va_arg(argptr,int)){
                printf("%d\n",ch);
        }
        printf("End:%d\n",ch);
        va_end(argptr);
}

int main(){
        myprintf(1,2,3,4,5,6,0,88888888);
        return 0;
}

运行结果

[[email protected] STDC]$ gcc stdc_va_list.c
[[email protected] STDC]$ ./a.out 
2
3
4
5
6
End:0

自行分析结果。

The bottom line, for me, is simple.

Let’s hope it doesn’t take 500 years for a discipline of testing to becomes the standard for software developers.

From:dayu

您好!请登录

合作网站快捷登录:
点击取消回复

已有0评论

    大宇博客

    点击这里给我发消息 点击这里给我发消息 点击这里给我发消息 点击这里给我发消息