首页 > 代码库 > C-字符串常量

C-字符串常量

相同的字符串常量是共享的,而且是不可写的。

 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.exestrings are shared!string are NOT writable!