[英]String copy function returns no value strcpy while converting NSString to c string
sipurl
和regurl
是我從UItextField
獲得的NSString
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.