Jeeva
Jeeva

Reputation: 1951

String copy function returns no value strcpy while converting NSString to c string

sipurl and regurl are NSString which I get from 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 has value while trying to copy constant char to char I use strcpy function it returns no value for pword. The same code works fine when testing in simulator but doesn't work when connected to iOS phone.

Upvotes: 3

Views: 337

Answers (1)

CRD
CRD

Reputation: 53000

This may or may not be the cause of your problem, but it is an error in your code:

You are taking an NSString, converting it into a UTF8 C-string, and then copying the C-string into a C-array sized according to the length of the NSString. This is wrong, the NSString and its UTF8 translation can be different lengths. You need to determine the length of the UTF8 string and allow for the end-of-string marker. For example:

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

and similarly for the other arrays.

HTH

Upvotes: 1

Related Questions