char * str = "Hello";
*(str+1) = '3';
cout<<str;
What I was trying to do there was to change the second character into ‘3’, turning it into H3llo
Why doesn’t it work?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is undefined behaviour. You cannot change literal.
To have a pointer to literal, it should be:
Then, to be able to change string, this should be, for example
The other option is to allocate dynamically the memory (using
mallocandfree)