So I have a string with multiple newline escape sequences within it. I would like to cut the string to the first n lines, like the head program from coreutils. For example say I have the string "a\nb\nc\n", I would want the first line so I would want my output string to be "a". In other cases where I needed to cut a string I simply placed a null terminator in the position where I wanted the string to end. However when I run something like this:
void main() {
char *str = "a\nb\nc\n";
str[1] = '\0';
}
I get a segfault. I really don't understand why. What can I do to fix this?