C,C++字符串比较时, strcmp和==有什么区别?


资料来源: https://zhidao.baidu.com/question/538567915.html 


注意看,以下的代码有什么区别?注意看注释


//filleData.name 的类型是 char name[260]
 if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
 {
   int i = 0; //程序可以运行到这里
 }

 if ((findData.name== ".") || (findData.name== ".."))
 {
   int i = 0; //程序不能运行到这里
 }

如上代码所示,strcmp和==的区别是:

C字符串其实就是一个字符数组,在用==运算符比较时,数组自动转化为指针,因此==运算符比较的是两个字符串的地址。而strcmp函数比较的是两个字符串内容……

猜你喜欢

转载自blog.csdn.net/Johnisjohn/article/details/88549590