char *s2 = "hello world";
+----------------+ +----------------------------------+
| s2 (棧上) | ---> | "hello world\0" (唯讀資料段) |
| 類型: char* | | 地址: 0x400000 (假設) |
| 值: 0x400000 | | 內容: h e l l o w o r l d \0 |
+----------------+ +----------------------------------+
s2[0] = 'H';` 導致未定義行為 (通常 Segfault)。printf("%s", s2); 從地址連續讀取直到 \0,輸出完整字串。printf("%p", s2); 輸出地址值 (如 0x400000)。printf("%c", *s2); 解引用得 'h'。char s3[] = "hello world"; // 棧上分配 12 位元組,內容可修改
// 等價於 char s3[12] = {'h','e','l','l','o',' ','w','o','r','l','d','\0'};
sizeof(s2) = 指標大小 (4/8 位元組)。sizeof(s3) = 12 位元組。僅讀字串用 const char *s = "...";;需修改時用陣列或 malloc + strcpy。