Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the other answer already states, passing <code>ap</code> to a v*() function leaves <code>ap</code> in an undetermined state. So, the solution is to not depend on this state. I suggest an alternative workaround.</p> <p>First, initialize ap as normal. Then determine the length of the first formatted string using <code>vsnprintf(NULL, 0, fstra, ap)</code>. Concatenate the format strings, reinitialize <code>ap</code>, and split the output using the predetermined length of the first formatted string.</p> <p>It should look something like the following:</p> <pre><code>void test(const char* fstra, const char* fstrb, ...) { char *format, *buf; char *a, *b; int a_len, buf_len; va_list ap; va_start(ap, fstrb); a_len = vsnprintf(NULL, 0, fstra, ap); va_end(ap); asprintf(&amp;format, "%s%s", fstra, fstrb); va_start(ap, fstrb); buf_len = vasprintf(&amp;buf, format, ap); va_end(ap); free(format); a = malloc(a_len + 1); memcpy(a, buf, a_len); a[a_len] = '\0'; b = malloc(buf_len - a_len + 1); memcpy(b, buf + a_len, buf_len - a_len); b[buf_len - a_len] = '\0'; free(buf); } </code></pre> <p>As also discussed in <a href="https://stackoverflow.com/a/7147733/429091">the other answer</a>, this approach does not separate positional <code>printf</code>-style placeholders (<code>"%1$s. I repeat, %1$s."</code>). So the documentation for the interface should clearly state that both format strings share the same positional placeholder namespace—and that if one of the format strings uses positional placeholders then both must.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload