c - Confusion with Pointers Increment -
char *str = "string" ; char *end = str; while(*(end+1) != '\0') {     end++; }  while(str < end) {     char temp = *str;     *str = *end;     *end = temp;     str++;     end--; }`   edit:  both these*str = *end,  *str++ = *end invalid?
the above code gives error @ line.
aren't str , end pointing read part in memory whether post increment?
char *str = "string" ;   is string literal placed in read-only data section.
in line:
*str = *end;   is trying modify string, segfault..
same *str++ = *end, both invalid.
to make code work, change
char *str = "string";   to
char str[] = "string";   but note can not use *str++ because array names constant (not modifiable lvalue)
Comments
Post a Comment