Sign in to follow this  
romanMagyar

easy char qt

Recommended Posts

romanMagyar    151
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
romanMagyar    151
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
twanvl    512
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this