Suppose we have char *a ="Mission Impossible";
If we give cout<<*(a+1), then the output is i.
Is there any way to change this value, or this is not possible?
Yes, there are several ways to do this, but you have to make a copy of the string first because if you didn't, you'd be modifying memory you're not allowed to (where string literals are stored).
const char* a = "Mission Impossible"; // const char*, not char*, because we can't // modify its contents char buf[80] = {}; // create an array of chars 80 large, all initialised to 0 strncpy(buf, a, 79); // copy up to 79 characters from a to buf cout << *(buf + 1); // prints i buf[1] = 'b'; cout << *(buf + 1); // prints b *(buf + 1) = 't'; cout << buf[1]; // prints t That said, if this exercise is not for learning purposes, it is highly recommended that you learn and use std::string rather than C-style strings. They are superior in almost every way and will result in far less frustration and errors in your code.
char a[] = "Mission Impossible"; a[1] = 'x'; String literals cannot be modified. Typically they are placed a section of the binary that will be mapped read-only, therefore writing to them generates a fault. (This is implementation-defined behavior, but this happens to be the most common implementation these days.)
By declaring the string as a character array it is writable. The other alternative would be to duplicate the string literal into heap memory, either through malloc, new, or std::string.
The simplest way to change that is doing *(a+1)='value_you_want';
This will change the content of a pointer (your case pointer is a+1) to the value you set.
*(a+1)?& to * as fge says, that's not legal code; you're not allowed to alter characters from character literals. It will work on some platforms and compilers, but not on others -- and it can have drastically bad effects.
const char* a = "mission possible"What do you expect the output to be? what do you want to change?