Sunday, 15 January 2012

Splitting string by '/' failed, C -



Splitting string by '/' failed, C -

this question has reply here:

modifying string literal [duplicate] 4 answers

i wanna split string '/' , alter char '/' '/0' in string, wrote function this:

void parse_query(char* str){ char* p = str; char** r = (char**)malloc(sizeof(char*)*5); int = 0; r[i++] = p; while(p=strchr(p,'/')){ *p = '/0'; p++; r[i++] = p; } }

when ran programme below:

char* s = "a/b"; parse_query(s);

the segmentation fault occurred @ line:

*p = '/0';

can give me suggestion?

when ran programme below: char* s = "a/b";

so modifying string literal "a/b", undefined behaviour. if want modify it, utilize array this:

char s[] = "a/b"; parse_query(s);

in addition, should (as noted antonh):

*p = '\0';

or *p = 0;

to terminate string. '/0' different '\0'.

c string split

No comments:

Post a Comment