简体   繁体   English

const char指针修改

[英]const char pointer modification

Can we assign one const char * to another and modify the value? 我们可以将一个const char *分配给另一个const char *并修改值吗?

const char * c1; 
const char * c2 = c1; 

Is the above assignment valid? 以上作业有效吗?

const char* means that the contents of the string are constant. const char*表示字符串的内容是常量。 You can have different pointers referring to the same constant memory. 您可以使用不同的指针来引用相同的常量内存。 In other words, the pointers are different variables with the same value. 换句话说,指针是具有相同值的不同变量。 Remember that the value of a pointer is the address of the memory to which it refers. 请记住,指针的值就是它所指向的内存的地址。

So, yes, the assignment above is perfectly valid. 因此,是的,上面的分配是完全有效的。


I wonder if this question is related to your previous question . 我想知道这个问题是否与您先前的问题有关 You say here: 您在这里说:

Can we assign one const char * to another and modify the value? 我们可以将一个const char *分配给另一个const char *并修改值吗?

If by "modify the value" you mean modify the contents of the string (that's what you wanted to do in the previous question), then no you cannot. 如果“修改值”是指修改字符串的内容(这是您在上一个问题中要执行的操作),那么不能。 In the example you give here, you have two pointers referring to the same constant block of memory. 在此处给出的示例中,您有两个指针指向相同的恒定内存块。 If one of the pointers cannot modify that memory, then neither can the other. 如果一个指针不能修改该内存,则另一个指针也不能。

const char* means that the string pointed to is constant. const char*表示指向的字符串是常量。

char const* means that the pointer itself is constant. char const*表示指针本身是常量。

Yes it is perfectly valid. 是的,它是完全有效的。

A const char * means the content the pointer points to is constant, but it doesnt stop the pointer from being assigned to another pointer because this doesn't change the content pointed to by the original pointer. const char *表示指针所指向的内容是常量,但是它不会阻止将该指针分配给另一个指针,因为这不会更改原始指针所指向的内容。

This gives you two pointers pointing to same memory location. 这为您提供了两个指向相同内存位置的指针。

    |----------|                                 
    |   c1     |                            
    |          |  1000                       
    |   2000   |                             
    |----------|                               
          |                                           
          |
          |
          |
          -------------------->|----------|
                               |   num    |
          -------------------->|          |  2000
          |                    |    2     |     
          |                    |----------|
          |
          |
    |----------|                             
    |   c2     |                            
    |          |  3000                       
    |   2000   |                             
    |----------|    

You can hack around and modify the content of a const char * pointer but note that modifying a variable declared as const results in Undefined Behavior . 您可以随意修改const char *指针的内容,但请注意,修改声明为const的变量会导致未定义行为

You know this already, but it's worthwhile repeating: the const qualifier is a directive to the compiler only . 您已经知道这一点,但是值得重复: const限定符编译器的指令。 It has nothing whatever to do with anything you feel like doing at run time . 它与您在运行时的感觉无关。 The sense of const is: "hey, mr. compiler: tell me whenever you notice that I modify this variable in a way that I don't mean to." const的含义是:“嘿,编译器先生:每当您注意到我以我不是故意的方式修改此变量时,请告诉我。” AFAIK, the compiler issues a warning when it sees such self-destructive usage. AFAIK,当编译器看到这种自毁用法时,会发出警告。 I suppose you might be able to get the compiler to issue a flat-out error in this case. 我想在这种情况下,您也许能够使编译器发出平坦的错误。

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

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