4

I am trying to understand the reason behind not being able to modify a string literal in C.

Why is the following illegal in C?

char* p = "abc";
*p = 'd';
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • It's not illegal, as you've written it. Your code will compile in `C` (and also in `C++`). – Adrian Mole Oct 27 '19 at 23:48
  • 8
    @Adrian: Just because it compiles doesn't mean it's legal/valid. It's not. – R.. GitHub STOP HELPING ICE Oct 27 '19 at 23:53
  • @R.. The statement, `char* p = "abc";` declares `p` as a pointer to an array of four characters; the statement, `*p = 'd';` assigns the value `d` to the first element of that array. What's illegal about that? – Adrian Mole Oct 28 '19 at 00:07
  • 3
    @Adrian: C 2018 6.4.5 7 says, of the array created by a string literal in source text, “If the program attempts to modify such an array, the behavior is undefined.” In C++, a string literal is an array of `const` characters, and the implicit conversion in `char *p = "abc";` from `const char *` to `char *` is not allowed. – Eric Postpischil Oct 28 '19 at 00:13
  • @EricPostpischil Interesting! Always happy to learn new stuff. I just tried this in MSVC: `char*p = "abc", q[4] = "abc"; *p = 'd'; *q = 'd'; printf("%s %s\n", p, q);` … it compiles and runs but outputs `abc dbc`. I think I have to surrender, on this one! – Adrian Mole Oct 28 '19 at 00:18
  • 2
    Is this really a duplicate? The other seems to be asking why something the asker expects to work isn't working. This question seems to be asking why the language is the way it is. See: "reason behind not being able to". – R.. GitHub STOP HELPING ICE Oct 28 '19 at 00:26

1 Answers1

10

From the C89 Rationale, 3.1.4 String literals:

String literals are specified to be unmodifiable. This specification allows implementations to share copies of strings with identical text, to place string literals in read-only memory, and perform certain optimizations. However, string literals do not have the type array of const char, in order to avoid the problems of pointer type checking, particularly with library functions, since assigning a pointer to const char to a plain pointer to char is not valid. Those members of the Committee who insisted that string literals should be modifiable were content to have this practice designated a common extension (see F.5.5).

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Is *this* legal: `char q[4] = "abc"; *q = 'd';`? – Adrian Mole Oct 28 '19 at 00:10
  • 1
    @Adrian yes, that's legal, because in that case a copy of the string literal is stored in `q`. What you are modifying is the copy of the string literal, not the string literal itself. – Pablo Oct 28 '19 at 00:18
  • @Pablo - See my comment in the OP's question: I was wrong, **R..** is right! – Adrian Mole Oct 28 '19 at 00:20
  • @Adrian I don't get what you mean. I see that R.. is right. But I answered your question in in the first comment of this answer. – Pablo Oct 28 '19 at 00:22
  • @Pablo I was admitting that I was wrong in my comment in the question (which prompted my comment here). I was never implying anything you said was wrong. Sorry to cause confusion. – Adrian Mole Oct 28 '19 at 00:24