3

May I know why this assignment is possible?

char const *c = "Hello";
c = "there!";

Isn't it pointing to a location whose contents cannot be modified. As far as I am able to make out, it is creating another object and making c point to that. Is it true.

Any other highlight upon this would be appreciated.

Saksham
  • 9,037
  • 7
  • 45
  • 73
  • You are not modifying the contents of `*c`; you are pointing to a new (and unchangeable) location... – Floris Jun 18 '13 at 04:26
  • 1
    `c` is a pointer to unmodifiable chars ... there's no reason it can't be pointed at different unmodifiable chars. "it is creating another object" -- not really; the string literal is already in memory (it's loaded when the program is loaded) and this simply sets `c` to its address. – Jim Balter Jun 18 '13 at 06:09

6 Answers6

5

Like many answers have already stated const char *c is read as follows:

c is a pointer to a const char

If you are not familiar with pointers, a pointer holds the address of some 'object' in memory.

Your two strings, "Hello" and "there!" are string literals. At compile time (minus compiler optimizations), they are placed in the text/rodata segment of the code. Not sure if you are familiar with the layout of an executable in memory (also known as the runtime environment), but you have the Text, Data, BSS, Heap and Stack.

At runtime, the char pointer is allocated on the stack and is first set to point to the area in memory where the string "Hello" is, and then in your second statement, the pointer is set to point to "there!"

Both of those strings are literally a contiquous block of bytes in memory and your pointer is just pointing to the start of one and then the other.

So even though it appears as if you were creating those strings on the stack, that actually isn't the case.

On the other hand, if you had declared the string as follows,

char ch[] = "Hello";

then you could not reassign ch to point to "There" because ch is a non-modifialble l-value in this case.

This goes a little beyond your question, but hopefully it will help you understand a little more of what the code looks like under the hood.

ffhaddad
  • 1,653
  • 13
  • 16
4

Yes, const applies to whatever is to the left of it (unless there is nothing to the left of it).

You're just pointing c to a different string.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 6
    No, `const` applies to whatever is to the left of it, unless there is nothing to the left of it, in which case it applies to whatever is to the right of it. In this case `const` applies to the `char`, not the `*`. – Carl Norum Jun 18 '13 at 04:27
0

char const *c = "Hello"; is a pointer to a constant. It means the content pointed by that pointer can't be modified, but the pointer can be modified to point different memory location.

Note "Hello" is a string literal. Even though char *c = "Hello"; is given, trying to modify content is undefined behaviour. i.e c[0] = 'x'.

Ken
  • 30,811
  • 34
  • 116
  • 155
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

Read from right to left to interpret the declaration correctly: c is a pointer to a constant char.

Ken
  • 30,811
  • 34
  • 116
  • 155
0
char const *c = "Hello";

This is a pointer to a constant,

NOT a constant pointer char * const c = "Hello";

Incase of a pointer to constant, the value pointed to can't be changed using the pointer like, *c[3] = 't'; etc.

It is in the case of constant pointer that you can't change what the pointer is pointing to. Like this,

c = "there!";

See this too.

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

When you code

       char const* c1 = "Hello";

"Hello" will be stored in the Read-Only memory(String literals go in Read Only memory) and Pointer c1 will be stored in the Stack.

So the Strings in Read-Only Memory (like "Hello" in this case)cannot be modified but YES you can make C1 to point to some other String stored in Read-Only Memory .

This is what you did in the second line of code.

Also, try same thing by just removing const keyword.

uni
  • 117
  • 4