0

我正在检索 va_args 但我不确定为什么它有时会返回 0、1、2 等等。当我尝试访问它时它会使我的程序崩溃,并且我无法断言它返回的内容。

有任何想法吗?

背景:我在 iOS 上编程。

void strngCls::Form(*wchar_t wCh, va_list argList)
{

    va_list argListTest = argList;
    const wchar_t *ws1 = va_arg(argListTest, const wchar_t *);
    const wchar_t *ws2 = va_arg(argListTest, const wchar_t *);

    if(ws1!=NULL && ws1!=(const wchar_t *)1 && ws2!=NULL)
    {
        int asd=wcslen(ws1);

        int asd2=wcslen(ws2);
        NSLog(@"ws1 = %d, ws2 = %d",asd,asd2);

        NSString *s1 = [[NSString alloc] initWithBytes:ws1 length:sizeof(wchar_t) *   wcslen(ws1) encoding: NSUTF32LittleEndianStringEncoding];
        NSLog(@"s1 %@",s1);
        NSString *s2 = [[NSString alloc] initWithBytes:ws2 length:sizeof(wchar_t) * wcslen(ws2) encoding: NSUTF32LittleEndianStringEncoding];
        NSLog(@"s2 %@",s2);


        NSString *iosString = [[NSString alloc] initWithFormat:@"%@ = %@", s1, s2];
        const char *ws3 = [iosString cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        [s1 release];
        [s2 release];

        x = vswprintf(m_pchData, GetAllocLength(), (LPCTSTR)ws3, argListTest);

    }
    else if(ws1!=NULL && ws1!=(const wchar_t *)1)
    {
        int asd=wcslen(ws1);

        NSLog(@"ws1 = %d",asd);

        NSString *s1 = [[NSString alloc] initWithBytes:ws1 length:sizeof(wchar_t) * wcslen(ws1) encoding: NSUTF32LittleEndianStringEncoding];
        NSLog(@"s1 %@",s1);
        NSString *iosString = [[NSString alloc] initWithFormat:@"%@", s1];
        const char *ws3 = [iosString cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
        [s1 release];
    }

}
4

1 回答 1

1

你错过了一个电话va_start

void strngCls::Form(*wchar_t wCh, va_list argList)
{
    va_list argListTest = argList;
    va_start(argListTest, wCh);
    const wchar_t *ws1 = va_arg(argListTest, const wchar_t *);
    const wchar_t *ws2 = va_arg(argListTest, const wchar_t *);

另外,va_end完成后打电话:

    va_end(argListTest);
}
于 2012-08-01T21:42:52.980 回答