爲什麼const char*
指向的值由char數組更新,該數組應該只保存應存儲在ROM中的原始字符串文本的副本。const char *仍然修改指向的值
我知道const char*
,char* const
,const char* const
的基本理論從這個鏈接const char * const versus const char *?
#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;
}
程序中沒有數組的副本。賦值不會複製數組。 –