You are trying to change read-only memory (where this string literal is stored). Instead, you can use a char array if you need to change this memory.
char str[] = "This is a string"; str[0] = 'S';
I have used this line of code many times.
I hope not. At best, you get segfault (I say โat bestโ because trying to change readonly memory is unspecified behavior, in which case something can happen, and an accident is the best thing that can happen).
When you declare a pointer to a string literal, it indicates that only memory is read in the data segment (look at the assembly if you want). Declaring your type as char [] will copy this literal in the function stack, which in turn will allow it to be changed if necessary.
Ed S. source share