다음 코드에서 문자열은 동일하지만 "문자열이 다릅니다"라는 메시지가 출력됩니다.문자 배열이 같지만 같지 않습니다.
논리 란 무엇입니까? 도 strcmp()
와
#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;
}
은 출력, 즉 은 "문자열이 다른"와 동일하다.
#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;
}
'strcmp()'은 문자열이 0이 아닌 경우 0을 반환하고 '0'은 false 조건이므로'if()'에'! '를 사용했기 때문에'0'을 반환합니다. 제 대답에 나와있는 문서 링크를 읽으십시오. 배울 노력을 기울이십시오. –