简体   繁体   中英

Assigning string literal to pointer to const char

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.

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.

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.

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. ie c[0] = 'x' .

从右到左阅读以正确解释声明: c是指向常量char的指针。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM