使用相等运算符 == 在 C 中比较两个字符串的相等性 [英] Using the equality operator == to compare two strings for equality in C
本文介绍了使用相等运算符 == 在 C 中比较两个字符串的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int main (int argc, **argv){if (argv[1] == "-hello")printf("真
");别的printf("假
");}
<前># ./myProg -hello错误的
为什么?我意识到 strcmp(argv[1], "-hello") == 0
返回 true...但为什么我不能使用相等运算符来比较两个 C 字符串?
解决方案
因为argv[1]
(例如)实际上是一个指向字符串的指针.所以你所做的只是比较指针.
int main (int argc, **argv)
{
if (argv[1] == "-hello")
printf("True
");
else
printf("False
");
}
# ./myProg -hello False
Why? I realize strcmp(argv[1], "-hello") == 0
returns true... but why can't I use the equality operator to compare two C strings?
解决方案
Because argv[1]
(for instance) is actually a pointer to the string. So all you're doing is comparing pointers.
这篇关于使用相等运算符 == 在 C 中比较两个字符串的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文