Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
If I have a pointer and I know the indexes of both the chars, how would I swap the chars(I didn’t actually allocate an array)
i.e.
char *str = "hello" and I know and I wanted to swap the ‘h’ and ‘o’ which is index 0 and 4, and then return the pointer as well.
I’m used to doing the convention of
temp = array[i];
array[i] = array[j];
array[j] = temp;
The following declaration:
Should be, to warn you about unsafe behaviors:
Because attempt to modify a string litteral is an undefined behavior according to C standard, you should rather use an array.
Then, swap the array’s elements is easy and safe: