<p>I am looking to do this in C/C++. I came across <a href="http://www.swig.org/Doc1.3/Varargs.html" rel="nofollow noreferrer"><strong>Variable Length Arguments</strong></a>, but this suggests a solution with Python and C using <a href="http://sourceware.org/libffi/" rel="nofollow noreferrer">libffi</a>.</p>
<p>Now, if I want to wrap the <code>printf</code> function with <code>myprintf</code>.</p>
<p>I do it like below:</p>
<pre><code>void myprintf(char* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    printf(fmt, args);
    va_end(args);
}

int _tmain(int argc, _TCHAR* argv[])
{
    int a = 9;
    int b = 10;
    char v = 'C';
    myprintf(&quot;This is a number: %d and \nthis is a character: %c and \n another number: %d\n&quot;, a, v, b);
    return 0;
}
</code></pre>
<p>But the results are not as expected!</p>
<pre class="lang-none prettyprint-override"><code>This is a number: 1244780 and
this is a character: h and
another number: 29953463
</code></pre>
<p>What did I miss?</p>

[{'answer_id': 41413, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 0, 'selected': False, 'text': '<p>How do you mean a pure C/C++ solution?</p>\n<p>The rest parameter (...) is supported cross platform in the C runtime.</p>\n<p><em><a href="https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/va-arg-va-copy-va-end-va-start" rel="nofollow noreferrer">va_arg, va_copy, va_end, va_start</a></em></p>\n'}, {'answer_id': 41414, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 1, 'selected': False, 'text': '<p>Are you using C or C++? The next C++ version, C++0x, will support <a href="http://en.wikipedia.org/wiki/Varargs#Variadic_functions_in_C.2C_Objective-C_and_C.2B.2B" rel="nofollow noreferrer">variadic templates</a> which provide a solution to that problem.</p>\n\n<p>Another workaround can be achieved by clever operator overloading to achieve a syntax like this:</p>\n\n<pre><code>void f(varargs va) {\n    BOOST_FOREACH(varargs::iterator i, va)\n        cout &lt;&lt; *i &lt;&lt; " ";\n}\n\nf(args = 1, 2, 3, "Hello");\n</code></pre>\n\n<p>In order to get this to work, the class <code>varargs</code> has to be implemented to override <code>operator =</code> that returns a proxy object which, in turn, overrides <code>operator ,</code>. However, making this variant type safe in current C++ isn\'t possible as far as I know since it would have to work by type erasure.</p>\n'}, {'answer_id': 41422, 'author': 'David Sykes', 'author_id': 259, 'author_profile': 'https://Stackoverflow.com/users/259', 'pm_score': 3, 'selected': False, 'text': '<p>I am also unsure what you mean by pure.</p>\n<p>In C++ we use:</p>\n<pre><code>#include &lt;cstdarg&gt;\n#include &lt;cstdio&gt;\n\nclass Foo\n{   \n    void Write(const char* pMsg, ...);\n};\n\nvoid Foo::Write( const char* pMsg, ...)\n{\n    char buffer[4096];\n    std::va_list arg;\n    va_start(arg, pMsg);\n    std::vsnprintf(buffer, 4096, pMsg, arg);\n    va_end(arg);\n    ...\n}\n</code></pre>\n'}, {'answer_id': 41794, 'author': 'Mark', 'author_id': 4405, 'author_profile': 'https://Stackoverflow.com/users/4405', 'pm_score': 7, 'selected': True, 'text': "<p>The problem is that you cannot use 'printf' with <em>va_args</em>.  You must use <strong>vprintf</strong> if you are using variable argument lists.  <em>vprint</em>, <em>vsprintf</em>, <em>vfprintf</em>, etc.  (there are also 'safe' versions in Microsoft's C runtime that will prevent buffer overruns, etc.)</p>\n<p>You sample works as follows:</p>\n<pre><code>void myprintf(char* fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    vprintf(fmt, args);\n    va_end(args);\n}\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n    int a = 9;\n    int b = 10;\n    char v = 'C';\n    myprintf(&quot;This is a number: %d and \\nthis is a character: %c and \\n another number: %d\\n&quot;, a, v, b);\n    return 0;\n}\n</code></pre>\n"}, {'answer_id': 10380279, 'author': 'john', 'author_id': 1053621, 'author_profile': 'https://Stackoverflow.com/users/1053621', 'pm_score': 0, 'selected': False, 'text': "<pre><code>void myprintf(char* fmt, ...)\n{\n    va_ list args;\n    va_ start(args, fmt);\n    printf(fmt, args); // This is the fault. &quot;vprintf(fmt, args);&quot;\n                       // should have been used.\n    va_ end(args);\n}\n</code></pre>\n<p>If you're just trying to call <em>printf</em>,\nthere's a <em>printf</em> variant called <em>vprintf</em> that takes\nthe <em>va_list</em> directly: <em>vprintf(fmt, args);</em></p>\n"}, {'answer_id': 17837382, 'author': 'Shafik Yaghmour', 'author_id': 1708801, 'author_profile': 'https://Stackoverflow.com/users/1708801', 'pm_score': 4, 'selected': False, 'text': '<p>In <a href="https://en.wikipedia.org/wiki/C%2B%2B11" rel="nofollow noreferrer">C++11</a>, this is one possible solution using <a href="http://en.cppreference.com/w/cpp/language/parameter_pack" rel="nofollow noreferrer">variadic templates</a>:</p>\n<pre><code>template&lt;typename... Args&gt;\nvoid myprintf(const char* fmt, Args... args)\n{\n    std::printf(fmt, args...);\n}\n</code></pre>\n<p>As <a href="https://stackoverflow.com/questions/41400/how-can-i-wrap-a-function-with-variable-length-arguments#comment26036756_41400">rubenvb points out</a>, there are trade-offs to consider. For example, you will be generating code for each instance which will lead to code bloat.</p>\n'}, {'answer_id': 22048209, 'author': 'basin', 'author_id': 447503, 'author_profile': 'https://Stackoverflow.com/users/447503', 'pm_score': 3, 'selected': False, 'text': "<p>Actually, there's a way to call a function that doesn’t have a <code>va_list</code> version from a wrapper. The idea is to use assembler, do not touch arguments on the stack, and temporarily replace the function return address.</p>\n<p>An example for Visual C x86. <code>call addr_printf</code> calls <code>printf()</code>:</p>\n<pre><code>__declspec( thread ) static void* _tls_ret;\n\nstatic void __stdcall saveret(void *retaddr) {\n    _tls_ret = retaddr;\n}\n\nstatic void* __stdcall _getret() {\n    return _tls_ret;\n}\n\n__declspec(naked)\nstatic void __stdcall restret_and_return_int(int retval) {\n    __asm {\n        call _getret\n        mov [esp], eax   ; /* replace current retaddr with saved */\n        mov eax, [esp+4] ; /* retval */\n        ret 4\n    }\n}\n\nstatic void __stdcall _dbg_printf_beg(const char *fmt, va_list args) {\n    printf(&quot;calling printf(\\&quot;%s\\&quot;)\\n&quot;, fmt);\n}\n\nstatic void __stdcall _dbg_printf_end(int ret) {\n    printf(&quot;printf() returned %d\\n&quot;, ret);\n}\n\n__declspec(naked)\nint dbg_printf(const char *fmt, ...)\n{\n    static const void *addr_printf = printf;\n    /* prolog */\n    __asm {\n        push ebp\n        mov  ebp, esp\n        sub  esp, __LOCAL_SIZE\n        nop\n    }\n    {\n        va_list args;\n        va_start(args, fmt);\n        _dbg_printf_beg(fmt, args);\n        va_end(args);\n    }\n    /* epilog */\n    __asm {\n        mov  esp, ebp\n        pop  ebp\n    }\n    __asm  {\n        call saveret\n        call addr_printf\n        push eax\n        push eax\n        call _dbg_printf_end\n        call restret_and_return_int\n    }\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/41400', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/123/']