【发布时间】:2011-09-14 04:33:59
【问题描述】:
可能重复:
What is the difference between char s[] and char *s in C?
这有什么区别:
char arr[] = "Hello, world!";
还有这个:
char *arr = "Hello, world!";
两个字符串的内存分配在哪里?为什么我无法更改后一个字符串的内容?
【问题讨论】:
-
严格来说,后者应该是
const char *arr = "hello world!"; -
@Nemo:它应该,但严格来说,C 不需要它。 C 字符串文字不是
const,但试图修改一个是未定义的行为 -
@teacher:这个问题已经被问过很多次了。快速搜索给出了这些链接。 (a)stackoverflow.com/questions/1704407/… (b)stackoverflow.com/questions/1880573/…