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