题目:写出以下代码输出。
#include<iostream>
using namespace std;
int main()
{
char str1[]="abc";
char str2[]="abc";
const char str3[]="abc";
const char str4[]="abc";
const char *str5="abc";
const char *str6="abc";
char *str7="abc";
char *str8="abc";
cout<<(str1==str2)<<endl;
cout<<(str3==str4)<<endl;
cout<<(str5==str6)<<endl;
cout<<(str7==str8)<<endl;
getchar();
}
结果是:
为神马呢?
写个代码来看看内存地址:
#include<iostream>
using namespace std;
int main()
{
char str1[]="abc";
char str2[]="abc";
const char str3[]="abc";
const char str4[]="abc";
const char *str5="abc";
const char *str6="abc";
char *str7="abc";
char *str8="abc";
printf("abc: %d\n",&("abc"));
printf("str1: %d\n",str1);
printf("str2: %d\n",str2);
printf("str3: %d\n",str3);
printf("str4: %d\n",str4);
printf("str5: %d\n",str5);
printf("str6: %d\n",str6);
printf("str7: %d\n",str7);
printf("str8: %d\n",str8);
getchar();
return 0;
}
结果是:
分析:
str1,str2,str3,str4是数组变量,它们有各自的空间,而数组名就是数组的首地址。
str5,str6,str7,str8是指针,它们指向了相同的常量区域。