如何传递参数从省略号运算符到其他函数? [英] How to pass argument got from ellipsis operator to other function?

查看:130
本文介绍了如何传递参数从省略号运算符到其他函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void myPrintf(const char* format, ...) {
    // some code
    va_list vl;
    printf(format, vl);
}

int main() {
    myPrintf("%d\n", 78);
}

在这段代码中,我试图将参数从省略号传递到 printf

In this code I have tried to pass the argument from ellipsis to printf. It compiles but prints garbage instead of 78. What is the right way of doing it?

推荐答案

您需要执行以下操作:

You need to do the following:

void myPrintf(const char *format, ...) {
    va_list vl;
    va_start(vl, format);
    vprintf(format, vl);
    va_end(vl);
}

请注意使用 vprintf 而不是 printf

这篇关于如何传递参数从省略号运算符到其他函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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