简体   繁体   English

为什么我可以更改const char *变量的值?

[英]Why can I change the value of a const char* variable?

Why does the following code in C work? 为什么C中的以下代码有效?

const char* str = NULL;
str = "test";
str = "test2";

Since str is a pointer to a constant character, why are we allowed to assign it different string literals? 由于str是指向常量字符的指针,为什么我们允许为它分配不同的字符串文字? Further, how can we protect str from being modified? 此外,我们如何保护str不被修改? It seems like this could be a problem if, for example, we later assigned str to a longer string which ended up writing over another portion of memory. 看起来这可能是一个问题,例如,我们后来将str分配给一个更长的字符串,最后写入另一部分内存。

I should add that in my test, I printed out the memory address of str before and after each of my assignments and it never changed. 我应该补充一点,在我的测试中,我在每个作业之前和之后打印出str的内存地址,但它从未改变过。 So, although str is a pointer to a const char, the memory is actually being modified. 因此,尽管str是指向const char的指针,但内存实际上正在被修改。 I wondered if perhaps this is a legacy issue with C? 我想知道这可能是C的遗留问题吗?

You are changing the pointer, which is not const (the thing it's pointing to is const). 你正在改变指针,它不是const(它指向的东西是const)。

If you want the pointer itself to be const, the declaration would look like: 如果您希望指针本身为const,则声明将如下所示:

char * const str = "something";

or 要么

char const * const str = "something";  // a const pointer to const char
const char * const str = "something";  //    same thing

Const pointers to non-const data are usually a less useful construct than pointer-to-const. 指向非const数据的const指针通常是一个不如指针到const的有用构造。

Further, how can we protect str from being modified? 此外,我们如何保护str不被修改?

char * const str1; // str1 cannot be modified, but the character pointed to can
const char * str2; // str1 can be modified, but the character pointed to cannot
const char * const str3 // neither str3 nor the character pointed to can be modified.

The easiest way to read this is to start from the variable name and read to the left: 读取此内容的最简单方法是从变量名称开始并向左读取:

  • str1 is a const ant pointer to a char acter str1是一个指向 字符const ant 指针
  • str2 is a pointer to a char acter const ant STR2指向一个char const的 ACTER蚂蚁
  • str3 is a const ant pointer to a char acter const ant STR3是一个const蚂蚁指针char const的 ACTER蚂蚁

NOTE: the right-to-left reading does not work in the general case, but for simple declarations it's a simple way to do it. 注意:从右到左的读数在一般情况下不起作用,但对于简单的声明,这是一种简单的方法。 I found a java applet based on code from "The C Programming Language" that can decipher declarations with a full explanation of how to do it. 我发现了一个基于“C编程语言”代码的java applet ,它可以解释声明,并详细说明如何进行。

On a related note, definitely take a look at " const pointer versus pointer to const ". 在相关的说明中,一定要看看“ const指针与指向const的指针 ”。 It helps with what some people call const correctness . 它有助于一些人称为const正确性 I keep it in my bookmarks so that I can refer to it every now and then. 我把它保存在我的书签中,这样我就可以不时地参考它。

Memory for the string literals are allocated on the stack, and all your assignments do are change the str pointer to point to those memory addresses. 字符串文字的内存在堆栈上分配,所有的分配都将str指针更改为指向那些内存地址。 The constant character it pointed to initially hasn't changed at all. 它最初指向的常数字符根本没有改变。

What you're looking for may be the syntax... 您正在寻找的可能是语法......

const char* const str = NULL;
str = "test";
str = "test2";

Notice the "const" after the char* which yields a compiler error when trying to compile/build. 注意char *之后的“const”,它在尝试编译/构建时产生编译器错误。

Besides, declaring a variable as const means that variable is read-only; 此外,将变量声明为const意味着变量是只读的; it does not mean the value is constant! 这并不意味着价值是恒定的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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