gpt4 book ai didi

objective-c - 为什么 va_arg 返回地址 0,然后是 1,然后是 2?

转载 作者:行者123 更新时间:2023-11-29 04:30:59 26 4
gpt4 key购买 nike

我正在检索 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];
}

}

最佳答案

您错过了对 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);
}

关于objective-c - 为什么 va_arg 返回地址 0,然后是 1,然后是 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11732074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com