Connecting Tech Pros Worldwide Forums | Help | Site Map

Very very simple question with Char * type

Newbie
 
Join Date: Oct 2006
Posts: 9
#1: Nov 15 '06
I get an error like this : Unhandled exception at 0x004115ae in LesPointeur2.exe: 0xC0000005: Access violation writing location 0x00417a28.

I see that i cannot overwrite the "Hello World" like it is read only. "Hello World"
is like a constant but I don't want to. I never declare char const *MyString so
why I cannot directly change a char with my pointer CharPtr ?

I just want to know how I can do it with pointers.
No array or string solution please


Expand|Select|Wrap|Line Numbers
  1.  
  2. char *MyString= "Hello World";
  3. char *CharPtr;
  4.  
  5. CharPtr= MyString;
  6. *CharPtr='m';
  7.  
  8. CharPtr++;
  9. *CharPtr='o';
  10.  
  11. cout<<MyString;
  12.  
Thanx
Chris3000

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,185
#2: Nov 15 '06

re: Very very simple question with Char * type


CharPointer is never pointed at modifiable data (it is not a good idea to modify string constants and on some systems causes errors).

MyString points to a string constant

you assign the value of MyString to CharPointer therefore CharPointer also points to a string constant.

If you want to modify what CharPointer is pointing to then it must point to modifiable memory.
Reply