IF语句始终为False [英] IF Statement Always False

查看:112
本文介绍了IF语句始终为False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个if语句,用于比较两个IP地址.它总是返回不相等.这些是值.

char * addr2
char ipSrc [20]

声明...

I have an if statement that compares two IP addresses. It''s always returning NOT EQUAL. Here are the values.

char* addr2
char ipSrc[20]

The statement...

if (addr2 == ipSrc)
	{
	printf("\n   EQUAL");
	}
else
	{
	printf("\n   NOT EQUAL");
	}



是类型还是语句?以及如何纠正呢?谢谢.



Is it the types or the statement? And how can this be corrected? Thank you.

推荐答案

这两个变量不能相等,即使字符串内容相同.更确切地说,您只能通过在本地上下文中分配addr2 = ipSrc来使它们相等.这就是为什么:两个变量都是指针.可以将第一个指针初始化或分配给某个对象的地址或堆中分配的内存的地址.第二个指针指向内存中的固定位置.如果这两个指针指向内存中的不同位置,则即使将完全相同的字符放在内存中,比较"=="也会始终返回false.比较运算符"=="比较指针,而不是内存内容.

正如hervebags建议的那样,您可以使用strcmp,但请记住,两个字符串都应以空值结尾.我建议改用std::string,请参见 http://www.cplusplus.com/reference/string/string/ [ ^ ].

—SA
Those two variables cannot be equal, even when the strings are identical in content. More exactly, you can only make them equal by assignment addr2 = ipSrc in local context. Here is why: both variables are pointers. First pointer can be initialized or assigned to the address of some object or to the address of memory allocated in heap; second pointer points to the fixed location in memory. If these two pointer point to different locations in memory, comparison "==" will always return false, even if you put exact same characters in these areas of memory. The comparison operator "==" compares pointers, not contents of memory.

As hervebags advised, you can use strcmp, but remembers that both strings should be null-terminated. I would advise to use std::string instead, see http://www.cplusplus.com/reference/string/string/[^].

—SA


在C ++中比较字符串时,最好使用"strcmp"而不是"==.
您的if语句应如下所示:

When comparing strings in C++, it is better to use "strcmp" instead of "==".

Your if statement should look like this:

if(strcmp(addr2,ipSrc) == 0 )
        {
                cout << "Equal\n";
        }
        else
        {
                cout << "Not equal\n";
        }




别忘了在顶部包含字符串.即:




Don''t forget to include string at the top. ie:

#include>



"strcmp"返回一个整数值,该整数值指示字符串之间的关系:
零值表示两个字符串相等.
大于零的值表示不匹配的第一个字符在str1中的值大于在str2中的值;小于零的值表示相反的意思. 查看此链接 [ ]

我还建议您这样声明"addr2":char addr2 [20]
因为在您的版本中,您正在尝试将字符串常量转换为字符.



"strcmp" returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite. Check out this link[]

I also suggest that you declare ''addr2'' like this: char addr2[20]
Because, in your version, you are trying to convert a string constant to a character.


很抱歉,输入错误.

我的意思是:

别忘了在顶部包含字符串.即:
Sorry, typo error.

I meant:

Don''t forget to include string at the top. ie:
#include <string.h>


这篇关于IF语句始终为False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆