各地传递可变数量的参数 [英] Passing variable number of arguments around

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

问题描述

说我有一个C函数,它接受一个可变数量的参数:我怎样才能调用它期望从它里面可变数量的参数,传递一个钻进了第一个函数

的所有参数另一个函数?

例如:

 无效FORMAT_STRING(字符* FMT,...);无效debug_print(INT dbg_lvl,字符* FMT,...){
    FORMAT_STRING(FMT,/ *我怎么通过所有从参数'...'* /?);
    fprintf中(标准输出,FMT);
 }


解决方案

要传递的省略号,你必须将它们转换成的va_list并使用va_list的在你的第二个功能。具体来说;

 无效FORMAT_STRING(字符* FMT,va_list的argptr,字符* formatted_string);
无效debug_print(INT dbg_lvl,字符* FMT,...)
{
 烧焦formatted_string [MAX_FMT_SIZE] va_list的argptr;
 的va_start(argptr,FMT);
 FORMAT_STRING(FMT,argptr,formatted_string);
 va_end用来(argptr);
 fprintf中(标准输出,%S,formatted_string);
}

Say I have a C function which takes a variable number of arguments: How can I call another function which expects a variable number of arguments from inside of it, passing all the arguments that got into the first function?

Example:

void format_string(char *fmt, ...);

void debug_print(int dbg_lvl, char *fmt, ...) {
    format_string(fmt, /* how do I pass all the arguments from '...'? */);
    fprintf(stdout, fmt);
 }

解决方案

To pass the ellipses on, you have to convert them to a va_list and use that va_list in your second function. Specifically;

void format_string(char *fmt,va_list argptr, char *formatted_string);


void debug_print(int dbg_lvl, char *fmt, ...) 
{    
 char formatted_string[MAX_FMT_SIZE];

 va_list argptr;
 va_start(argptr,fmt);
 format_string(fmt, argptr, formatted_string);
 va_end(argptr);
 fprintf(stdout, "%s",formatted_string);
}

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

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