Passing variable number of arguments around

前端 未结 11 750
我寻月下人不归
我寻月下人不归 2020-11-22 07:16

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 a

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 07:31

    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);
    }
    

提交回复
热议问题