在下面的代码中,虽然字符串相同,但我得到的输出是“字符串不同”
逻辑是什么?
#include <iostream>
using namespace std;
main(void)
{
char str1[30]="India",str2[30]="India";
cout<<"str1 address :"<<str1<<endl;
cout<<"str2 address :"<<str2<<endl;
if(str1==str2)
cout<<"strings same"<<endl;
else
cout<<"strings are different"<<endl;
}
即使使用strcmp()
,输出也是相同的,即“字符串不同”。
#include <iostream>
#include <string.h>
using namespace std;
main(void)
{
char str1[30]="India",str2[30]="India";
cout<<"str1 address :"<<str1<<endl;
cout<<"str2 address :"<<str2<<endl;
if(strcmp(str1,str2))
cout<<"strings same"<<endl;
else
cout<<"strings are different"<<endl;
}