Forward an invocation of a variadic function in C

前端 未结 12 2708
悲哀的现实
悲哀的现实 2020-11-22 06:41

In C, is it possible to forward the invocation of a variadic function? As in,

int my_printf(char *fmt, ...) {
    fprintf(stderr, \"Calling printf with fmt %         


        
12条回答
  •  天命终不由人
    2020-11-22 07:31

    Almost, using the facilities available in :

    #include 
    int my_printf(char *format, ...)
    {
       va_list args;
       va_start(args, format);
       int r = vprintf(format, args);
       va_end(args);
       return r;
    }
    

    Note that you will need to use the vprintf version rather than plain printf. There isn't a way to directly call a variadic function in this situation without using va_list.

提交回复
热议问题