A bus error occurs because in many (if not most or all modern) C compilers, string literals are allocated in read-only memory.
You change the string into place. In the first piece of code that you are trying to write to a string literal. Not a good idea.
In the second case, you malloc'd a string that puts it in a heap. It is now safe to cancel this line in place.
ADDITION
To a commenter who asked about segfaults and bus errors, this is a great question. I have seen both. Here is the bus error on mac:
$ cat bus.c char* s = "abc"; int main() {s[0]='d'; return 0;} $ gcc --version bus.c && ./a.out i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659) Copyright (C) 2007 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Bus error
On other operating systems / compilers you can get segfault.
Ray toal
source share