const char 指针修改

发布于 2024-12-09 08:36:01 字数 125 浏览 0 评论 0原文

我们可以将一个 const char * 分配给另一个并修改其值吗?

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

上述作业有效吗?

Can we assign one const char * to another and modify the value?

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

Is the above assignment valid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

っ〆星空下的拥抱 2024-12-16 08:36:01

const char* 表示字符串的内容是常量。您可以使用不同的指针来引用同一常量内存。换句话说,指针是具有相同值的不同变量。请记住,指针的值是它所引用的内存的地址。

所以,是的,上面的作业是完全有效的。


我想知道这个问题是否与您的上一个问题相关。你在这里说:

我们可以将一个 const char * 分配给另一个并修改其值吗?

如果“修改值”是指修改字符串的内容(这就是您在上一个问题中想要做的),那么不可以。在此处给出的示例中,有两个指针引用同一常量内存块。如果其中一个指针无法修改该内存,则另一个也不能。

const char* means that the contents of the string are constant. 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?

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.

清欢 2024-12-16 08:36:01

const char* 表示指向的字符串是常量。

char const* 表示指针本身是常量。

const char* means that the string pointed to is constant.

char const* means that the pointer itself is constant.

梨涡少年 2024-12-16 08:36:01

您已经知道这一点,但值得重复一下:const 限定符仅是针对编译器的指令。它与您在运行时想做的任何事情都没有任何关系。 const 的含义是:“嘿,编译器先生:每当您发现我以非本意的方式修改此变量时,请告诉我。” AFAIK,当编译器看到这种自毁性的用法时会发出警告。我想在这种情况下你也许能够让编译器发出一个彻底的错误。

You know this already, but it's worthwhile repeating: the const qualifier is a directive to the compiler only. 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." AFAIK, the compiler issues a warning when it sees such self-destructive usage. I suppose you might be able to get the compiler to issue a flat-out error in this case.

世俗缘 2024-12-16 08:36:01

是的,这是完全有效的。

const char * 表示指针指向的内容是常量,但它不会阻止指针被分配给另一个指针,因为这不会更改原始指针指向的内容。

这为您提供了两个指向同一内存位置的指针。

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

您可以破解并修改 const 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文