title: C varargs - va_copy issues
body: <p>I'm writing a function in C that takes a variable number of arguments.</p>
  
  <pre><code>size_t myprintf(char *fmt, ...);
  </code></pre>
  
  <p>So far, so good. I've decided it's best to do things the Right Way™ and make a version that takes variable arguments, and another version that takes a <code>va_list</code>.</p>
  
  <pre><code>size_t myprintf(char *fmt, ...);
  size_t myvprintf(char *fmt, va_list args);
  </code></pre>
  
  <p>Not that hard to do. Except <code>my_vprintf()</code> needs to send its <code>args</code> out to two different functions (first to <code>snprintf()</code> with a length of 0 to determine how much room we need, then to <code>sprintf()</code> after we've allocated that much room). I do this with <code>va_copy</code>.</p>
  
  <pre><code>size_t myvprintf(char *fmt, va_list args)
  {
      va_list args2;
      va_copy(args, args2);
      // do stuff with args2
      va_end(args2);
      // do more stuff with args
  }
  </code></pre>
  
  <p>This is all fine and dandy, but C99 is a bit poorly implemented. I would like, if possible, for my code to work in C89 as well, and to work with as many compilers and on as many platforms as possible. I currently have this after <code>#include &lt;stddef.h&gt;</code> but before any code:</p>
  
  <pre><code>#ifndef va_copy
  # ifdef __va_copy
  #  define va_copy(a,b) __va_copy(a,b)
  # else /* !__va_copy */
  #  define va_copy(a,b) ((a)=(b))
  # endif /* __va_copy */
  #endif /* va_copy */
  </code></pre>
  
  <p>I am led to believe that <code>((a)=(b))</code> is unreliable, and that I should perhaps use <code>memcpy()</code> or something similar, but this is still on the level of "If you don't support C99, I hope it works" rather than "If you don't support C99, never fear" (which is what I want). Is there any good way to get around this limitation? I've seen a few solutions - <code>va_list</code> functions that eat one argument and recurse, passing the <code>va_list</code> twice so that two separate copies are made, etc. - but I don't know how well they would work (and the recursive solution won't do so well if I just want to call <code>vsnprintf()</code>, now, will it?).</p>
  
  <p>So I turn to you, StackOverflow User. Is there anything more I can do to provide C89 compatibility, or are users without <code>va_copy</code> and <code>__va_copy</code> (admittedly few and far between) just going to have to suck it in and take it?</p>