C,处理可变参数函数 [英] C, Dealing with variable argument functions

查看:56
本文介绍了C,处理可变参数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想做这样的事情

void my_printf(char *fmt,...) {
 char buf[big enough];
 sprintf(buf,fmt,...);  
}

将可变数量的参数直接传递给具有接受可变参数的函数的正确方法是什么?

What is the proper way of passing the variable number of arguments directly to a function with accepts variable arguments?

推荐答案

sprintf具有称为vsprintfva_list形式.将您在本地构造的va_list作为最后一个参数传递给它.

sprintf has a va_list form called vsprintf. Pass the va_list you construct locally to it as the last argument.

void my_printf(char *fmt,...) {
 va_list ap;
 va_start(ap, fmt);

 char buf[big enough];
 vsprintf(buf,fmt,ap);

 va_end(ap);
}

这篇关于C,处理可变参数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆