字符数组和字符常量的区别

来源:互联网 发布:听歌识曲软件哪个好 编辑:程序博客网 时间:2024/05/18 06:25
#include<iostream>using namespace std;int main(){char str1[]="hello word";char str2[]="hello word";char *str3 ="hello word";char *str4 ="hello word";if(str1==str2){cout<<"str1 and str2 are the same"<<endl;}else{cout<<"str1 and str2 not same"<<endl;}if(str3==str4){cout<<"str3 and str4 are the same"<<endl;}else{cout<<"str3 and str4 not same"<<endl;}system("pause");return 0;}

 字符数组和常量字符串

为了节省内存C++把常量字符串放到单独的一个内存区域,当几个指针赋值给相同的常量字符串时,它们实际上指向的是相同的地址.

但是用常量初始化数组就完全不同.

两个操作系统系统会分配两个12个字节的栈空间.


C++的内存分配图






0 0