user3457200
user3457200

Reputation: 129

c: issue with strchr(): returning wrong value?

I'm trying to do some code stuff within strchr but unfortuantly my use of strchr was leading to execution bugs, it appears that strchr in some cases returning wrong value, here is the code:

int main(){

    char* s="1/2/3/4/8/9/7";
    char r[100];
    char chunk2[100];
    int i,jpos;

    for(i=0;i<5;i++){
    strcpy(r, strrev(s));
    jpos = strchr(r, '/')-r;
    strncpy(chunk2, r, jpos);
    strcpy(r, strrev(chunk2));
    }
}

what's wrong? and how can I fix the issue? thanks.

Upvotes: 2

Views: 121

Answers (1)

this
this

Reputation: 5300

s is a string literal, strrev() tries to change it, you cannot do that on static data.

Use an array of characters

char s[]="1/2/3/4/8/9/7";

Upvotes: 3

Related Questions