va_start、va_arg、va_end、va_copy 可变参函数
1、应用与原理
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
2、va_start、va_arg、va_end、va_copy介绍
#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);
typedef struct {
char *a0; /* pointer to first homed integer argument */
int offset; /* byte offset of next parameter */
} va_list;
*fmt, ...) 中的fmt。通过va_start初始化ap,我们就获得了可变参数前一个参数fmt的地址。
3、Example
//foo.c
#include <stdarg.h>
#include <stdio.h>
void
foo(char *fmt, ...)
{
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while (*fmt)
switch (*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
/* need a cast here since va_arg only
/* need a cast here since va_arg only
takes fully promoted types */
c = (char) va_arg(ap, int);
printf("char %c\n", c);
break;
}
va_end(ap);
}
//main.c
#include <stdio.h>
#include <stdarg.h>
#include "foo.h"
int
main(void)
{
foo("%s %d %c %d %c", "Hello", 4, 'x', 3, 'y');
return 0;
}
windeal@ubuntu:~/Windeal/apue$ ./exe
string Hello
int 4
char x
int 3
char y
windeal@ubuntu:~/Windeal/apue$
//foo.c
#include <stdarg.h>
#include <stdio.h>
void
foo(char *fmt, ...)
{
va_list ap;
int len = 0;
char buf[64];
va_start(ap, fmt);
len = vsnprintf(buf, 128, fmt, ap);
va_end(ap);
int i = 0;
for(i = 0; i < len; i++)
{
putchar(buf[i]);
}
return ;
}
// main.c
#include <stdio.h>
#include <stdarg.h>
#include "foo.h"
int
main(void)
{
foo("Test:%s %d %c %d %c\n", "Hello", 4, 'x', 3, 'y');
return 0;
}
windeal@ubuntu:~/Windeal/apue$ ./exe
Test:Hello 4 x 3 y
va_start、va_arg、va_end、va_copy 可变参函数va_start、va_arg、va_end、va_copy 可变参函数
va_start、va_arg、va_end、va_copy 可变参函数的更多相关文章
- va_start,va_arg,va_end的使用
一.在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表. void fun(...); void fun(parm_list,...); #include <stdi ...
- va_list/va_start/va_arg/va_end深入分析【转】
转自:http://www.cnblogs.com/justinzhang/archive/2011/09/29/2195969.html va_list/va_start/va_arg/va_end ...
- C++省略参数(va_list va_start va_arg va_end)的简单应用
原文参考自:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html #include <iostream> #in ...
- va_list/va_start/va_arg/va_end深入分析
http://www.cnblogs.com/justinzhang/archive/2011/09/29/2195969.html
- va_start(),va_end()函数应用【转】
转自:http://www.cnblogs.com/gogly/articles/2416833.html 原理解释: VA_LIST 是在C语言中解决变参问题的一组宏,在<stdarg.h&g ...
- va_start(),va_end()函数应用
原理解释: VA_LIST 是在C语言中解决变参问题的一组宏,在<stdarg.h>头文件下. VA_LIST的用法: (1)首先在函数里定义一具VA_LIST型的变 ...
- va_list、va_start和va_end使用
我们知道va_start,va_arg,va_end是在stdarg.h中被定义成宏的,由于1.硬件平台的不同 2.编译器的不同,所以定义的宏也有所不同. 在ANSI C中,这些宏的定义位于stdar ...
- va_start和va_end使用详解
本文主要介绍va_start和va_end的使用及原理. 介绍这两个宏之前先看一下C中传递函数的参数时的用法和原理: 1.在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表 ...
- [转载]va_start和va_end使用详解
va_start和va_end使用详解 原文地址:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html 本文主要介绍va_s ...
随机推荐
- linux中uptime命令获取主机运行时间和查询系统负载信息
系统中的uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行了多长时间.目前有多少登陆用户.系 ...
- vue2.0中配置文件路径
在build/webpack.base.conf.js中添加一些代码即可 module.exports = { resolve: { extensions: ['.js', '.vue', '.jso ...
- Jquery中val、text、html的区别
html就是你可以添加像<a></a>.<p></p>等标记text只能写文本如果写了上面的标记则会以文本形式输出val是属性,只有有该属性的对象才能调 ...
- windows上面非常好用的辅助软件
1.everything 快速查找本地文件 下载地址:http://www.voidtools.com/
- React绑定this的三种方式
React可以使用React.createClass.ES6 classes.纯函数3种方式构建组件.使用React.createClass会自动绑定每个方法的this到当前组件,但使用ES6 cla ...
- [Deep Learning] 神经网络基础【转】
本文转载自:http://www.cnblogs.com/maybe2030/p/5597716.html 阅读目录 1. 神经元模型 2. 感知机和神经网络 3. 误差逆传播算法 4. 常见的神经网 ...
- linux之kali系统ssh服务开启
1.修改sshd_config文件,命令为:vi /etc/ssh/sshd_config 2.将#PasswordAuthentication no的注释去掉,并且将NO修改为YES //我的ka ...
- [P1860]新魔法药水
题目描述 商店里有N种药水,每种药水都有一个售价和回收价.小S攒了V元钱,还会M种魔法,可以把一些药水合成另一种药水.他一天可以使用K次魔法,问他一天最多赚多少钱? 输入输出格式 输入格式: 第一行四 ...
- 使用Spring注解注入属性
本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...
- Mysql CASE WHEN 用法
select sum(1) as col_0_0_, sum(case vciinfo.useable when -1 then 1 else 0 end) as col_1_0_, sum(case ...