【发布时间】:2015-09-22 13:56:36
【问题描述】:
为什么const char* 指向的值被一个字符数组更新,该数组应该只保存应该存储在 ROM 中的原始字符串文字的副本。
从这个链接const char * const versus const char *?知道const char*、char* const、const char* const的基本理论
#include <stdio.h>
#include <stdlib.h>
int main(){
char a[] = "ABCD";
char z[] = "WXYZ";
const char* b = a;
a[1] = 'N'; // WHY THIS WORKS AND UPDATES THE VALUE IN B.... a should make its own copy of ABCD and update
// it to NBCD... b should still point to a read only memory ABCD which can't be changed4
//b[1] = 'N'; // THIS FAILS AS DESIRED
printf("%s\n", b); // Output -> ANCD
return 0;
}
【问题讨论】:
-
您的程序中没有数组的副本。赋值不会复制数组。