0

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?

4
  • 1
    first, it has to be const char* a = "mission possible" What do you expect the output to be? what do you want to change? Commented Dec 17, 2011 at 19:30
  • 1
    You can change a lot of values all over the place, but you really need to be more specific to get sensible results. Commented Dec 17, 2011 at 19:33
  • What do you mean by "change this value"? Commented Dec 17, 2011 at 20:51
  • @ Karl ... I meant to say that if I need to change the value of any character within "Mission Impossible". Now I understand that since it is a constant pointer, this is impossible. Commented Dec 17, 2011 at 21:25

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2
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.

Comments

1

No, the char* a is actually read-only and if you try to modify the content you will get undefined behavior. You should ideally declare a as const char*.

Comments

0

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.

5 Comments

I suppose you meant *(a+1)?
@fge isn't & supposed to modify the content of a pointer? If no then this is what I meant :)
This will work if the data is definitely mutable; if it might be a string constant like this, it's not a good idea, though.
Even if you correct the & 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.
Thanks for the correction guys. I didn't know about that until now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.