0

I'm having a problem with va_ methods, and I couldn't find an example (or didn't figure out what the keywords are). The problem is, I need to use the same args for different formats, but the compiler gives me:

incorrect usage of va_start

error. The code I'm trying is something like this:

void vSaveNecessaryFields(EnumA eFormat, ...)
{
    va_list xArgs, xArgs2;
    const char *fmt1 = NULL, *fmt2 = NULL;
    char caString[100] = {0};

    fmt1 = cpGetDbFormat(eFormat);
    fmt2 = cpGetPrinterFormat(eFormat);

    va_start(xArgs1, fmt1);
    va_copy(xArgs2, xArgs1);
    vsnprintf(caString, sizeof(caString), fmt1, xArgs1);

    vSaveToDb(caString);

    va_start(xArgs2, fmt2);
    vsnprintf(caString, sizeof(caString), fmt2, xArgs2);
    vPrintFormatted(caString);
    va_end(xArgs2);
    va_end(xArgs1);
}

How can I solve this problem?

3
  • va_start(xArgs1, fmt1) --> va_start(xArgs1, eFormat) Commented Oct 3, 2016 at 8:51
  • trying it now. who downvoted this and why? Commented Oct 3, 2016 at 8:57
  • it worked. write it as an answer and I'll accept it. Commented Oct 3, 2016 at 9:26

2 Answers 2

2

The argument to va_start should be the eFormat argument. Furthermore, the va_list is declared as xArgs but you use xArgs1, which causes a syntax error.

void vSaveNecessaryFields(EnumA eFormat, ...) {
    va_list xArgs, xArgs2;
    const char *fmt1 = NULL, *fmt2 = NULL;
    char caString[100] = {0};

    fmt1 = cpGetDbFormat(eFormat);
    fmt2 = cpGetPrinterFormat(eFormat);

    va_start(xArgs, eFormat);
    vsnprintf(caString, sizeof(caString), fmt1, xArgs);
    va_end(xArgs);

    vSaveToDb(caString);

    va_start(xArgs2, eFormat);
    vsnprintf(caString, sizeof(caString), fmt2, xArgs2);
    vPrintFormatted(caString);
    va_end(xArgs2);
}
Sign up to request clarification or add additional context in comments.

1 Comment

xArgs -> xArgs1 was a typo of mine when I simplified the code :)
0

You need to call va_end, then call va_start a second time after you have closed the parameter block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.