Strings (differences between an array of char and *char)
Hello,
Could please someone explain me why these two senteces don't cause a execution error
char letters[]="ABCDE";
letters[0]='X';
whereas these two sentences do cause and error in execution time?
char* letters2="ABCDE";
*(letters2)='X';
I understand that what I am doing in the first block is to create an array called letters with the values A,B,C,D,E and /n.
In the second block, from what I have understood from my manual, you are creating a pointer to char, and at the same time, you are initialiazing the value for that memory position with the letter A, and the following positions with B,C,D,E, and /n.
But what I don't understand is why I cannot access the position pointed by letters2, and change the first letter.
Thank you in advance for your help.
|