The problem here is that you try to modify a string literal (str
points to that) and string literals are constant (i.e. read-only, non modifiable).
Trying to modify something constant is undefined behavior. Undefined behavior may sometimes seem to work, but most of the time it will cause a crash at runtime.
A good advice to find possible cases of undefined behaviors (or other things that might be bad) is to enable more warnings. Then you should get a warning about assigning a constant array (all string literals are constant arrays) to a non-constant pointer.