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

 

posted on 2014-08-18 21:15  醉清风JM  阅读(168)  评论(0)    收藏  举报

导航