Code A:
char* str="hello";
str[2]='!';
printf("%s\n", str);
In this code, the program compiles and runs but get a run-time error: "
However, if one writes the following code (Code B):
char str[10]="hello";
str[2]='!';
printf("%s\n", str);
Then, everything is fine. The program runs and outputs "he!lo".
I don't understand how the difference between code A and code B affect the behaviour of the compiler.
As far as I know, a pointer to an array points to the first element in the array (meaning, to the place where the character 'h' is located, and the elements of any array can change using the line: str[2]='!';
In code B, this line works fine! So... Why isn't it good in code A ?