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

参考:链接

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/archive/2012/11/17/2774480.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/69736

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午1:57
下一篇 2023年2月9日 下午1:57

相关推荐