字符串的赋值问题

//判断字符串是否相等
char str1[] = "Hello World!";
char str2[] = "Hello World!";


char *str3 = "Hello World!";
char *str4 = "Hello World2!";




if (str1 == str2) //==只能比地址,(不能比较内容)
printf("str1 and str2 are same\n");
else
//两个字符串数组,分配两个长度相等的空间,将“Hello World!”复制进去,因此两个初始的地址不同
printf("str1 and str2 are not same\n"); 


if (strcmp(str1,str2))
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");


if (str3==str4)
   //两个指针指向相同的地址空间(同一块地址空间)存值为"Hello world!"
printf("指针:str3 and str4 are same\n");
else

printf("指针:str3 and str4 are not same\n");


if (!strcmp(str3, str4))
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");


//总结:==运算符比较的是两个字符串的地址。而strcmp函数比较的是两个字符串内容
 /*strcmp 函数说明:
   设这两个字符串为str1,str2,
   若str1 = str2,则返回零;
若str1<str2,则返回负数;
若str1>str2,则返回正数。*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值