By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Very very simple question with Char * type

P: 9
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
Nov 15 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
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.
Nov 15 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.