说有答案的都是在坑人.这个问题是标准规定的不定义的行为.每个编译器的作者都可以自行决定如何实现字符串常量的存储.比如把字符串存到一个远程数据库,或者存到只读ROM而不是内存里也是可以的.
ISO/IEC 14882:2003 2.13.
2.13. String literals
[...]An ordinary string literal has type “array of n const char” and static storage duration (3.7)
Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation- defined. The effect of attempting to modify a string literal is undefined.
一个可以进行的编译器优化是为str1分配空间之后将str指向str1的第二个字符,这样两个字符串可以共享空间.