gpt4 book ai didi

ios - 字符串复制函数在将 NSString 转换为 c 字符串时不返回任何值 strcpy

转载 作者:搜寻专家 更新时间:2023-10-30 20:24:13 24 4
gpt4 key购买 nike

sipurlregurlNSString 我从 UItextField

得到的
    const char *sipAddr = [sipUrl cStringUsingEncoding:NSUTF8StringEncoding];
const char *regAddr = [regUrl cStringUsingEncoding:NSUTF8StringEncoding];
const char *uname = [orgUsername cStringUsingEncoding:NSUTF8StringEncoding];
const char *pword = [password cStringUsingEncoding:NSUTF8StringEncoding];
//const char *pword = [password UTF8String];
const char *realm = [realmStr cStringUsingEncoding:NSUTF8StringEncoding];

//char *sipAddrArr=strdup(sipAddr);
//char *regAddrArr=strdup(regAddr);
//char *unamearr=strdup(uname);
//char *pwordarrnew = strdup(pword);

char sipAddrArr[[sipUrl length]];
strcpy(sipAddrArr,sipAddr);

char regAddrArr[[regUrl length]];
strcpy(regAddrArr,regAddr);

char unamearr[[orgUsername length]] ;
strcpy(unamearr, uname);

char pwordarrnew[[password length]];
strcpy(pwordarrnew,pword);

char realmchr[[realmStr length]];
strcpy(realmchr,realm);
char pwordarr1[] = {"10000"};
printf("\nPassword value sent: %s",pword);
printf("\nPassword value sent: %s",pwordarr1);

pword 在尝试将常量 char 复制到 char 时具有值 我使用 strcpy 函数它不返回 pword 的值。相同的代码在模拟器中测试时工作正常,但在连接到 iOS 手机时不起作用。

最佳答案

这可能是也可能不是您问题的原因,但它是您代码中的一个错误:

您正在服用 NSString ,将其转换为 UTF8 C 字符串,然后将 C 字符串复制到根据 NSString 的长度调整大小的 C 数组中.这是错误的,NSString及其 UTF8 翻译可以是不同的长度。您需要确定 UTF8 字符串的长度并考虑字符串结束标记。例如:

char sipAddrArr[strlen(sipAddr)+1];
strcpy(sipAddrArr,sipAddr);

其他数组也类似。

HTH

关于ios - 字符串复制函数在将 NSString 转换为 c 字符串时不返回任何值 strcpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40845028/

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