相同的字符串常量是共享的,而且是不可写的。
1 #include <iostream> 2 using namespace std; 3 4 char *string1, *string2; 5 6 int main() { 7 string1 = "abcd"; string2 = "abcd"; 8 if(string1 == string2) printf("strings are shared!\n"); 9 else printf("strings are NOT shared!\n"); 10 //有可能运行时错误 11 //string1[0] = ‘1‘; 12 if(*string1 == ‘1‘) printf("string are writable!\n"); 13 else printf("string are NOT writable!\n"); 14 15 return 0; 16 }
输出:
$ ./a.exe strings are shared! string are NOT writable!
时间: 2024-10-26 17:34:40