0

I have a pointer and I pass it to a string using sprintf but when I try to access elements in the string I have trouble.

What I want is if I have a pointer to the string abcde to create a new string

seg[5]="a,b,c,d,e"

such that when I call seg[0] I get "a".

I have some code below

     char *pch5;
     char seg1[128];

     pch5=strtok(seg,",");
                                if(pch5!=NULL){
                                sprintf(seg1,"%s",pch5);
                                    printf("%s",seg1[0]);
                                }
1

2 Answers 2

1

Your printf is wrong, you are using the format %s, but what you want is %c.

0

seg[5] here holds string "a,b,c" only, because strlen("a,b,c") == 5. But if you want the full string use is as following:

seg[] = "a,b,c,d,e";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.