8

i'm trying to remove const-ness from a variable (char*), but for some reason when i try to change the value, the original value of the const variable still remains the same.

 const char* str1 = "david";
 char* str2 = const_cast<char *> (str1);
 str2 = "tna";

now the value of str2 changes but the original value of str1 remains the same, i've looked it up on Google but couldn't find a clear answer.

when using const_cast and changing the value, should the original of the const variable change as well ?

David Faizulaev
  • 4,651
  • 21
  • 74
  • 124

2 Answers2

10

The type of str1 is const char*. It is the char that is const, not the pointer. That is, it's a pointer to const char. That means you can't do this:

str1[0] = 't';

That would change the value of one of the const chars.

Now, what you're doing when you do str2 = "tna"; is changing the value of the pointer. That's fine. You're just changing str2 to point at a different string literal. Now str1 and str2 are pointing to different strings.

With your non-const pointer str2, you could do str2[0] = 't'; - however, you'd have undefined behaviour. You can't modify something that was originally declared const. In particular, string literals are stored in read only memory and attempting to modify them will bring you terrible misfortune.

If you want to take a string literal and modify it safely, initialise an array with it:

char str1[] = "david";

This will copy the characters from the string literal over to the char array. Then you can modify them to your liking.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Okay, so is there a way i can change the value of the const char* variable using casting ? – David Faizulaev Mar 12 '13 at 10:07
  • @DavidFaiz You can modify the pointer just fine without the cast. You *can't* modify the `char` if you've set the pointer to point at a string literal, which you have. String literals are constant. See my latest edit for how to copy a string literal to an array. – Joseph Mansfield Mar 12 '13 at 10:08
  • yea just seen it and got it ! thank you very much for the quick reply ! so basically i cannot modify the literal string but just use the const char variable and point to a diffrent one. – David Faizulaev Mar 12 '13 at 10:10
  • Only literals are originally declared as const right? – Tahlil Jun 25 '14 at 04:31
  • @DavidFaiz if this has fully answered your question, you should consider marking it as the accepted answer, by clicking the check mark on the left side. – marcusshep Aug 10 '17 at 12:52
3

str2 is simply a pointer. And your code just changes the value of the pointer, the address, not the string to which it points.

What's more, what you are attempting to do leads to undefined behaviour, and will most likely result in runtime errors. All modern compilers will store your string "david" in read-only memory. Attempts to modify that memory will lead to memory protection errors.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Compilers verify at compile time if const variables are being used out of its context or use tricks(conditionally) when on runtime to avoid it. What you're saying about it writing to read only memory seems debased, maybe you tried to say the above? Compilers should not write to ROM, to begin with, only very well tested software or data pieces user intend to record on something like a CD-ROM should be directed to be written on such since it's a very limited compared to RAM or other volatiles resources. – Marcelo Oct 09 '21 at 03:39
  • @marcelo The variable is not const, the data to which it points is const. And here when we talk about read only memory it is RAM, but the virtual memory system is used to mark this block of memory as being read only. Are you familiar with virtual memory and protection? – David Heffernan Oct 09 '21 at 07:17
  • I get what you mean, I thought it was hardware read-only memory at first, but it's about literal machine instruction or some layer which simulates the read-only value. – Marcelo Oct 19 '21 at 18:19