I am trying to edit a string after initialization using character literals, as follows:
int main() {
char str1[10] = "hello";
str1[0] = "b";
printf("%s\n", str1);
return 0;
}
The result is "dello" i.e. a "d" not a "b". Likewise the below just gives nonsense.
int main() {
char str1[10];
str1[0] = "h";
str1[1] = "e";
str1[2] = "l";
str1[3] = "l";
str1[4] = "o";
printf("%s\n", str1);
}
I found one StackOverflow post that mentioned that this should cause a segfault or an Access-Violation error, but it doesn't really explain why.