关于==的问题

kurorohisoka 2009-02-09 09:43:36
==不是比较地址的吗?如下代码。

#include <iostream>
#include <string>
using namespace std;

int main( )
{
char str1[] ="ab";
char str2[] ="ab";
const char str3[] = "ab";
const char str4[] = "ab";
const char* str5 = "ab";
const char* str6 = "ab";
string str7="ab";
string str8="ab";
cout<<(str1==str2)<<endl;
cout<<(str3==str4)<<endl;
cout<<(str5==str6)<<endl;
cout<<(str7==str8)<<endl;

cout<<&str1<<endl;
cout<<&str2<<endl;
cout<<&str3<<endl;
cout<<&str4<<endl;
cout<<&str5<<endl;
cout<<&str6<<endl;
cout<<&str7<<endl;
cout<<&str8<<endl;
}

输出:0
0
1
1
0012FF50
0012FF44
0012FF38
0012FF2C
0012FF20
0012FF14
0012FEEC
0012FEC4

地址都不一样为什么会输出两个1,这不是表示地址相同才是1的吗?小弟不明,忘大侠解决!
...全文
221 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
Feng_Fing 2009-02-14
  • 打赏
  • 举报
回复
学习了...
fbmsyu 2009-02-14
  • 打赏
  • 举报
回复
str1==str2 本来就不能比较,数组不支持这种比较.所以是假的.
还是看书吧先
coolsin 2009-02-14
  • 打赏
  • 举报
回复
我把程序稍微修改了一下:
#include <iostream>
#include <string>
using namespace std;

int main( )
{
char str1[] ="ab";
char str2[] ="ab";
const char str3[] = "ab";
const char str4[] = "ab";
const char* str5 = "ab";
const char* str6 = "ab";
string str7="ab";
string str8="ab";

cout <<(str1==str2) <<endl;
cout <<(str3==str4) <<endl;
cout <<(str5==str6) <<endl;
cout <<(str7==str8) <<endl;

cout <<"&str1:"<<&str1 <<endl;
cout <<"&str2:"<<&str2 <<endl;

cout <<"str1:"<<str1 <<endl;
cout <<"str2:"<<str2 <<endl;

cout <<"&str3:"<<&str3 <<endl;
cout <<"&str4:"<<&str4 <<endl;

cout <<"str3:"<<str3 <<endl;
cout <<"str4:"<<str4 <<endl;

cout <<"&str5:"<<&str5 <<endl;
cout <<"&str6:"<<&str6 <<endl;

cout <<"str5:"<<str5 <<endl;
cout <<"str6:"<<str6 <<endl;

cout <<"&str7:"<<&str7 <<endl;
cout <<"&str8:"<<&str8 <<endl;

cout <<"str7:"<<str7 <<endl;
cout <<"str8:"<<str8 <<endl;

}

运行结果为:
0
0
1
1
&str1:0x22ff50
&str2:0x22ff40
str1:ab
str2:ab
&str3:0x22ff30
&str4:0x22ff20
str3:ab
str4:ab
&str5:0x22ff1c
&str6:0x22ff18
str5:ab
str6:ab
&str7:0x22ff00
&str8:0x22fef0
str7:ab
str8:ab


cout <<(str1==str2) <<endl;
//这里是比较地址吗?是因为地址不同所以输出0吗?

cout <<"&str1:"<<&str1 <<endl;
cout <<"&str2:"<<&str2 <<endl;
//这两语也是输出地址吗?为什么加不加"&",都表示的是地址呢?

cout <<"str1:"<<str1 <<endl;
cout <<"str2:"<<str2 <<endl;
//这两句为什么是输出值呢?

哪位能解释一下
coolsin 2009-02-14
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 fbmsyu 的回复:]
str1==str2 本来就不能比较,数组不支持这种比较.所以是假的.
还是看书吧先
[/Quote]

数组名不是可以自动转换为指针吗?
zqz981 2009-02-14
  • 打赏
  • 举报
回复
基础帖! 温故知新
捕鲸叉 2009-02-13
  • 打赏
  • 举报
回复
调用运算符==,你可以看看它的源码,肯定不是比较其地址的

operator==
template<class E, class T, class A>
bool operator==(
const basic_string<E, T, A>& lhs,
const basic_string<E, T, A>& rhs);
template<class E, class T, class A>
bool operator==(
const basic_string<E, T, A>& lhs,
const E *rhs);
template<class E, class T, class A>
bool operator==(
const E *lhs,
const basic_string<E, T, A>& rhs);
Each template function overloads operator== to compare two objects of template class basic_string. All effectively return basic_string<E, T, A>(lhs).compare(rhs) == 0.

See the related sample program.
malganis00 2009-02-10
  • 打赏
  • 举报
回复
str7跟str8是类类型,string类里的operate==是重载过的了
str5跟str6还是基本类型!
ohxf1234 2009-02-10
  • 打赏
  • 举报
回复
// 正如楼上各位兄弟所说,(str5,str6)其分配的地址是相同的

#include <iostream>
#include <string>
using namespace std;

int main( void )
{
char str1[] ="ab";
char str2[] ="ab";
const char str3[] = "ab";
const char str4[] = "ab";
const char* str5 = "ab";
const char* str6 = "ab";
string str7="ab";
string str8="ab";
cout <<(str1==str2) <<endl;
cout <<(str3==str4) <<endl;
cout <<(str5==str6) <<endl;
cout <<(str7==str8) <<endl;

printf("%p\n", &(*str5) ); // 输出0046E028
printf("%p\n", &(*str6) ); // 输出0046E028

system("pause");
return 0;
}
soldierluo 2009-02-10
  • 打赏
  • 举报
回复
cout < <&str1 < <endl;
cout < <&str2 < <endl;
cout < <&str3 < <endl;
cout < <&str4 < <endl;
cout < <&str5 < <endl;
cout < <&str6 < <endl;
cout < <&str7 < <endl;
cout < <&str8 < <endl;
你这些是输出指针的地址而不是指针指向的地址
soldierluo 2009-02-10
  • 打赏
  • 举报
回复

cout < <&str5 < <endl;
cout < <&str6 < <endl;
cout < <&str7 < <endl;
cout < <&str8 < <endl;
改成
printf("%x\n",str5);
printf("%x\n",str6);
printf("%x\n",str7);
printf("%x\n",str8);
试试吧,就可以看到了
zhh157 2009-02-10
  • 打赏
  • 举报
回复
楼主可能是想问下面这种形式比较地址不同

char str1[] ="ab";
char str2[] ="ab";

cout < <(str1==str2) < <endl;


同样是比较地址,而这种形式比较地址却相同

const char* str5 = "ab";
const char* str6 = "ab";

cout < <(str5==str6) < <endl;


诚如楼上各位所说的
第一种形式,在栈上分别为str1和str2分配数组空间,这两个地址空间肯定不一样。然后对数组赋值"ab",str1和str2分别指向数组的首地址

第二种形式,也在栈上为str5和str6分配空间,但是只分配一个指针空间,而指针里面的内容都是指向文字常量"ab"的地址,因此它们两个的内容是一样的

楼主还需理解str5与&str5的区别
ysysbaobei 2009-02-10
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

int main( )
{
char str1[] ="ab";
char str2[] ="ab";
const char str3[] = "ab";
const char str4[] = "ab";
const char* str5 = "ab";
const char* str6 = "ab";
string str7="ab";
string str8="ab";
cout << (str1 == str2) << endl; // 0
cout << (str3 == str4) << endl; // 0
cout << (str5 == str6) << endl; // 1
cout << (str7 == str8) << endl; // 1

cout << "******************" << endl;

cout << &str1 << endl; // 0012FF70
cout << &str2 << endl; // 0012FF6C
cout << &str3 << endl; // 0012FF68
cout << &str4 << endl; // 0012FF64
cout << &str5 << endl; // 0012FF60
cout << &str6 << endl; // 0012FF5C
cout << &str7 << endl; // 0012FF4C
cout << &str8 << endl; // 0012FF3C

cout << "******************" << endl;

cout << str1 << endl; // ab
cout << str2 << endl; // ab
cout << str3 << endl; // ab
cout << str4 << endl; // ab
cout << str5 << endl; // ab
cout << str6 << endl; // ab
cout << str7 << endl; // ab
cout << str8 << endl; // ab

return 0;
}

str1~str8 不全都是 ab 吗?
应该输出4个1才对哦???
Vegertar 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ICEQQ 的回复:]
char str1[] ="ab";
char str2[] ="ab";
比较2个地址,不等。

const char str3[] = "ab";
const char str4[] = "ab";
比较2个地址,不等。

const char* str5 = "ab";
const char* str6 = "ab";
表示指向常量"ab"的地址,相等.

string str7="ab";
string str8="ab";
c++ 重载了== , 所以==可以用来比较两个字符串相等(java不行)
[/Quote]

楼主应该是对
const char* str5 = "ab";
const char* str6 = "ab";
表示指向常量"ab"的地址,相等.

有些疑惑吧,根据exceptional c++中的说法,
Comparing pointers into string literals is undefined. The reason is that the standard explicitly allows compilers to store string literals in overlapping areas of memory as a space optimization. For this reason, it's entirely possible to take a pointer into two different string literals and have them compare equal.

大意是标准允许编译器将字符串字面值存储在重叠的内存区域来作为空间优化,所以完全存在两个不同的字符串指针比较结果相等.
soldierluo 2009-02-10
  • 打赏
  • 举报
回复
char str1[] ="ab";
char str2[] ="ab";
比较2个地址,不等, 因为str1和str2分配了空间,空间不同自然就地址不同了

const char str3[] = "ab";
const char str4[] = "ab";
比较2个地址,不等,同上,只是分配的空间中的值不能改变

const char* str5 = "ab";
const char* str6 = "ab";
表示指向常量"ab"的地址,相等,因为str5和str6仅仅是指针,没有分配存储数据的空间,根据编译器优化将指向“静态存储区的常量”,因此相等

string str7="ab";
string str8="ab";
同str5\6
nullah 2009-02-10
  • 打赏
  • 举报
回复
up
ysysbaobei 2009-02-10
  • 打赏
  • 举报
回复
mark,学习一下
xmu_才盛 2009-02-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 kurorohisoka 的回复:]
可是看后面输出的地址没有一个相同的
[/Quote]

任意两个变量的地址都不可能相同

就拿题目的例子来说

0012FF20 ---> 地址a --->"ab"
| | |
&str5 str5 *str5

0012FF14 ---> 地址b --->"ab"
| | |
&str6 str6 *str6

既然*str5与*str6都是常量,那么系统只保存一个常量值,即只有一个指向该常量的指针
因此 str5和str6 中存的就是这个指针,即地址a==地址b
因此 str5==str6
肥多罗 2009-02-09
  • 打赏
  • 举报
回复
&str5,&str6是指针本身的地址,肯定是不同的啦,比如定义int a,b;a,b地址不一样,而str5与str6指向的内容一样

指针里面存储的数值被解释成为内存里的一个地址。
要搞清一个指针需要搞清指针的四方面的内容:指针的类型、指针所指向的
类型、指针的值或者叫指针所指向的内存区、指针本身所占据的内存区。。。。。&str5指的是指针本身的
kurorohisoka 2009-02-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ICEQQ 的回复:]
char str1[] ="ab";
char str2[] ="ab";
比较2个地址,不等。

const char str3[] = "ab";
const char str4[] = "ab";
比较2个地址,不等。

const char* str5 = "ab";
const char* str6 = "ab";
表示指向常量"ab"的地址,相等.

string str7="ab";
string str8="ab";
c++ 重载了== , 所以==可以用来比较两个字符串相等(java不行)
[/Quote]

可是看后面输出的地址没有一个相同的
kurorohisoka 2009-02-09
  • 打赏
  • 举报
回复
可是看后面输出的地址没有一个相同的
加载更多回复(7)

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧