字符串比较必须使用strcmp

char s1[]="this"
char *s2 = "this"
if(s1=="this"){
   printf("s1 is equal to 'this' \n");  
}
if(s2=="this"){
   printf("s2 is equal to 'this'\n");
}

注意,不会输出"s1 is equal to 'this' \n",会输出"s2 is equal to 'this'\n",因为s=="this"比较的实际上是内存地址。
所以如果比较字符串是否相同,还是应该使用strcmp(x1,x2)。返回值等于x1-x2;

posted on 2015-12-05 13:36  jxzheng  阅读(262)  评论(0编辑  收藏  举报

导航