easy char qt

Started by
3 comments, last by twanvl 19 years, 5 months ago
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?
Advertisement
Where does sentence come from?
If a plant cannot live according to its nature, it dies; so a man.
Hmmm... to me it looks like sentence[0] and sentence[1] haven't been initialized.
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?
You cannot change literal strings. You should copy the string first.
char str[] = "Hello";str[0] = str[1];cout << str[0] << endl;

This topic is closed to new replies.

Advertisement