You cannot compare arrays of characters with ==, if you try and they are not the same array, then that is false.
for example,
- Code: Select all
char [] one = "a\0";
char [] two = "a\0";
if (one == two)
{
// the address is the same
}
if (one == one)
{
// the address is the same
}
if (strcmp(one, two) == 0)
{
// the values are the same
}
You may be asking "why the f do I have to use strcmp(x, y)
== 0?
Because strcmp is a lexical comparison. If x is < y then you will get a negative result from strcmp(x, y), zero for equality and a positive number for y > x.