-1

char *char_ptr = "anisha";

char char_arr[]= "anisha";

What is the "reason" that the contents of first memory location cannot be modified but the contents of second memory can?

Why is the first memory storage a constant string whereas the second one isn't?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

3 Answers3

3

First is a stack-allocated pointer to the first item at array of chars allocated at some immutable storage for the string literal. Pointer itself is mutable, but the data it points to is not.

Second is a stack-allocated array of 7 chars that is only initialized with data from string literal. Whole array is mutable because it is allocated on the stack.

user7860670
  • 35,849
  • 4
  • 58
  • 84
3

For C++

Because pointer and array are different things.

For the 1st case, char_ptr is a pointer points to a string literal, whose contents can't be modified. And trying to modify them via the pointer is UB. (That's why from C++11 we have to write it as const char *char_ptr = "anisha";.)

Attempting to modify a string literal results in undefined behavior: they may be stored in read-only storage (such as .rodata) or combined with other string literals:

For the 2nd case, char_arr is an array, whose contents are copied from the string literal. The contents is owned by the array itself and then it's fine to be modified.

String literals can be used to initialize character arrays. If an array is initialized like char str[] = "foo";, str will contain a copy of the string "foo".

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 1
    I want to know WHY behind all this. – Aquarius_Girl Jan 06 '18 at 13:45
  • @Aquarius_Girl what do you mean by "why behind this"? – Pablo Jan 06 '18 at 13:50
  • 1
    @Aquarius_Girl Could you make your questiong more specific? What's your expected result? Pointer should behave like array? or vice versa? – songyuanyao Jan 06 '18 at 13:52
  • Why can't string literal's contents be modified? @Pablo Why does compiler/linker stop us from modifyin it? – Aquarius_Girl Jan 06 '18 at 13:53
  • 2
    @Aquarius_Girl For C++, the standard says that modifying on string literal is UB. The compiler doesn't have to do anything to stop such things, UB means the compiler could do anything. – songyuanyao Jan 06 '18 at 14:00
  • Well I have been trying to do the same thing with C11 standard...suprisingly standard doesn;t say about string literals being in read only directly - it mentions non modifiable and also one place it mentions that *the storage can be read only...* but not directly. Do you see any hard wrong information on my answer? I hope not. Just curious about this thats why this comment and also you are quoting standard that's why. – user2736738 Jan 06 '18 at 14:43
  • There's some discussion in the question "[Why are C string literals read-only?](https://softwareengineering.stackexchange.com/questions/294748/why-are-c-string-literals-read-only)". – David A Jan 06 '18 at 14:47
  • @coderredoc Well, what I'm quoting is not from the standard. I think it's enough for the standard (both C and C++) to say that trying to modify string literals is UB. How the string literals are stored should be implementation details. And, I'm not familiar with C, but I don't think there're hard wrong things in your answer. – songyuanyao Jan 06 '18 at 14:50
  • @songyuanyao.: Well thats for sure...that is what standard says but really I was curious about that. I will make it more succint because really how it stores that implementations business - emulating the behavior should be as per standard wants. Thanks for the feedback. – user2736738 Jan 06 '18 at 14:53
2

Because the first one is a string literal whose address you are assigning. And string literals are non modifiable. Though it looks similar to the second case it isnn't. The string literal which is an array decays into pointer which is assigned to the char*.

Second one is simply initializing a local char array. Here it is just initializing the element of the array with that of string literal. And this is modifiable.

char char_arr[]= {'a','n','i','s','h','a','\0'};

And this is modifiable as a normal char array can be.

From C11 standard 6.4.5p7

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

user2736738
  • 30,591
  • 5
  • 42
  • 56