Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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...
C++
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.
Posted
Comments
LanFanNinja 9-Nov-11 20:34pm    
What is the value of ipSrc and what does addr2 point to??
Member 7766180 9-Nov-11 20:55pm    
They are both IP addresses as strings.

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
 
Share this answer
 
v2
Comments
Member 7766180 9-Nov-11 21:17pm    
Are you saying use this instead?

if (addr2.compare(ipSrc) != 0)
cout << addr2 << " is not " << ipSrc << "\n";
Sergey Alexandrovich Kryukov 9-Nov-11 21:25pm    
No, I did not say that. Use comparison of std::string. Still "==", but not for pointers.
--SA
LanFanNinja 9-Nov-11 21:19pm    
+5 good explanation
Sergey Alexandrovich Kryukov 9-Nov-11 21:26pm    
Do you think so? Thank you very much, but OP did not get it. My bad :-(
--SA
Member 7766180 9-Nov-11 21:28pm    
You mean change the pointers into strings, and then use == ?
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" 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.
 
Share this answer
 
Comments
Member 7766180 9-Nov-11 21:07pm    
This works well, except I can't change addr2 to char addr2[20] VS 2010 is kicking and screaming!
Sorry, typo error.

I meant:

Don't forget to include string at the top. ie:
#include <string.h>
 
Share this answer
 
v3
Comments
Albert Holguin 9-Nov-11 21:03pm    
You can improve your initial solution instead of posting an additional solution. See the links in your first solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900