Search code examples
c++printfvariadic-functionswidestring

va_args breaks wide string functions (swprintf) in XCode


In Xcode only (visual studio is fine), I am seeing swprintf break if you attempt to put it in a wrapper function using va_args.

Simplified example:

void test( wchar_t *a_buffer, int a_buffer_size, const wchar_t* a_format, ...)
{
    va_list args;
    va_start(args, a_format);
    ::swprintf(a_buffer, a_buffer_size, a_format, args );
    va_end(args);
}

double value = 1.0;
wchar_t text[32];
::swprintf( text, 32, L"%f", value ); // this works (text=L"1.0000")

test(text, 32, L"%f", 30.0); // this does not work (text=L"0.0000")

Any help appreciated, it's quite a stumper. I'm assuming the issue is with some quirk of XCode.

I have already messed with Locale Settings and file properties as suggested in this question: swprintf fails with unicode characters in xcode, but works in visual studio but it has not yielded any change, it looks like a separate issue.

Thanks.


Solution

  • If you want to pass a va_list object, then use

    int vswprintf(
        const wchar_t* buffer,
        size_t bufsz,
        const wchar_t* format, 
        va_list vlist );
    

    The trailing ... just means its argument could vary in length, but it doesn't mean that you could pass a va_list object directly.