c++字符串数组问题 20
#include<iostream>#include<string>usingnamespacestd;intmain(){chara1[]={"helloworld"}...
#include<iostream>
#include<string>
using namespace std;
int main()
{
char a1[]={"helloworld"};
cout<<a1<<endl;
char b1[]={'h','e','l','l','o','w','o','r','l','d'};
cout<<b1<<endl;
char c1[]={'h','e','l','l','o','w','o','r','l','d','\0'};
cout<<c1<<endl;
char b2[11]={'h','e','l','l','o','w','o','r','l','d'};
cout<<b2<<endl;
char c2[11]={'h','e','l','l','o','w','o','r','l','d','\0'};
cout<<c2<<endl;
if (b1==c1){cout<<"b1=c1"<<endl;}else{cout<<"b1!=c1"<<endl;}
if (b2==c2){cout<<"b2=c2"<<endl;}else{cout<<"b2!=c2"<<endl;}
if (b1==b2){cout<<"b1=b2"<<endl;}else{cout<<"b1!=b2"<<endl;}
if (c1==c2){cout<<"c1=c2"<<endl;}else{cout<<"c1!=c2"<<endl;}
if (c1==b2){cout<<"c1=b2"<<endl;}else{cout<<"c1!=b2"<<endl;}
}
这里为何它们不相等呀?输出结果是相同的呀?
解释为何???
谢谢!!! 展开
#include<string>
using namespace std;
int main()
{
char a1[]={"helloworld"};
cout<<a1<<endl;
char b1[]={'h','e','l','l','o','w','o','r','l','d'};
cout<<b1<<endl;
char c1[]={'h','e','l','l','o','w','o','r','l','d','\0'};
cout<<c1<<endl;
char b2[11]={'h','e','l','l','o','w','o','r','l','d'};
cout<<b2<<endl;
char c2[11]={'h','e','l','l','o','w','o','r','l','d','\0'};
cout<<c2<<endl;
if (b1==c1){cout<<"b1=c1"<<endl;}else{cout<<"b1!=c1"<<endl;}
if (b2==c2){cout<<"b2=c2"<<endl;}else{cout<<"b2!=c2"<<endl;}
if (b1==b2){cout<<"b1=b2"<<endl;}else{cout<<"b1!=b2"<<endl;}
if (c1==c2){cout<<"c1=c2"<<endl;}else{cout<<"c1!=c2"<<endl;}
if (c1==b2){cout<<"c1=b2"<<endl;}else{cout<<"c1!=b2"<<endl;}
}
这里为何它们不相等呀?输出结果是相同的呀?
解释为何???
谢谢!!! 展开
3个回答
展开全部
字符数组用strcmp比较
==比的是首地址
==比的是首地址
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这里羡举滚相等是内存相等.通常情况两次答盯地址比较不会相同,但C/C++有常量池机制.如果char *s1="helloworld",char* s2="helloworld"那么s1==s2.其它情况兄余都不会相等.
要比较两字符串内容,请用strcmp函数
要比较两字符串内容,请用strcmp函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询