• Advertisement
Sign in to follow this  

easy char qt

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I haven't programmed in a LONG time, so bear with me... in c++, I do: char *str = "Hello"; sentence[0] = sentence[1]; cout << sentence[0] << endl; Gives me an error (more like it crashes). What's up with that?

Share this post


Link to post
Share on other sites
Advertisement
Hmmm... to me it looks like sentence[0] and sentence[1] haven't been initialized.

Share this post


Link to post
Share on other sites
sorry, it should have been:
char *str = "Hello";
str[0] = str[1];
// something along those lines...
// whenever I assign an individual character from
// str to another character, the program crashes
cout << str[0] << endl; // program crashes!

This seems like the most basic thing you could possibly do, yet it crashes! What's wrong with it?

Share this post


Link to post
Share on other sites
You cannot change literal strings. You should copy the string first.

char str[] = "Hello";
str[0] = str[1];
cout << str[0] << endl;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement