Passing variable number of arguments around

前端 未结 11 745
我寻月下人不归
我寻月下人不归 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:29

    You can use inline assembly for the function call. (in this code I assume the arguments are characters).

    void format_string(char *fmt, ...);
    void debug_print(int dbg_level, int numOfArgs, char *fmt, ...)
        {
            va_list argumentsToPass;
            va_start(argumentsToPass, fmt);
            char *list = new char[numOfArgs];
            for(int n = 0; n < numOfArgs; n++)
                list[n] = va_arg(argumentsToPass, char);
            va_end(argumentsToPass);
            for(int n = numOfArgs - 1; n >= 0; n--)
            {
                char next;
                next = list[n];
                __asm push next;
            }
            __asm push fmt;
            __asm call format_string;
            fprintf(stdout, fmt);
        }
    

提交回复
热议问题