View Single Post
  #1 (permalink)  
Old October 17th, 2009, 08:43 AM
gabberfrombcn gabberfrombcn is offline
Registered User
 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
Reply With Quote