字符指针不分配存储区,字符常量存储于静态数据区

参考:链接

C++ 字符数组有存储区,其值为存储区首地址;字符指针不分配存储区吗,"abc" 以常量形式存于静态数据区,指针指向该区首地址。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     char s1[] = "abc";
 8     char s2[] = "abc";
 9 
10     const char s3[] = "abc";
11     const char s4[] = "abc";
12 
13     const char *s5 = "abc";
14     const char *s6 = "abc";
15 
16     char *s7 = "abc";
17     char *s8 = "abc";
18 
19     cout << boolalpha << (s1 == s2) << endl;
20     cout << boolalpha << (s3 == s4) << endl;
21     cout << boolalpha << (s5 == s6) << endl;
22     cout << boolalpha << (s7 == s8) << endl;
23 
24     return 0;
25 }

Output:

1 false
2 false
3 true
4 true

进入int main()后:

完成赋值后:

原文地址:https://www.cnblogs.com/submarinex/p/2774480.html