V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
paparika
V2EX  ›  Linux

c 语言,如何实现可变参数函数调用可变参数函数

  •  
  •   paparika · 2018-07-13 10:55:13 +08:00 · 1931 次点击
    这是一个创建于 2107 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如想实现一个函数 foobar(const char *format, ...){ printf(); }

    只是在 printf 外面包裹一层,这里面怎么实现

    2 条回复    2018-07-13 11:14:28 +08:00
    missdeer
        1
    missdeer  
       2018-07-13 11:02:10 +08:00   ❤️ 1
    paparika
        2
    paparika  
    OP
       2018-07-13 11:14:28 +08:00
    谢楼上,结贴
    #include <stdarg.h>

    void printf_wrapper(const char *format, ...)
    {
    va_list args;
    va_start(args, format);

    vprintf(format, args);//不可以是 printf!

    va_end(args);
    }

    #define printf_wrapper_macro(format,args...) \
    { \
    printf(format, ## args); \
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5626 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 06:23 · PVG 14:23 · LAX 23:23 · JFK 02:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.