Originally Posted by
shyjuu
because x is declared as array and z is not so
Yeah, but that is somewhat incomplete. Go further: the left hand side expression of the == is of type int. The right hand side expression is of type int*, since the array is converted to an int*, then when you add the int to that, the resulting expression is still an int*. Hence, this compares an int with an int*.
The thing is, syntactically, the statement is valid. We can refer to the syntax from Clause 6.5.9 of C99:
Code:
equality-expression:
relational-expression
equality-expression == relational-expression
I do not want to delve too deeply into this, so take my word that both z and x + y are relational expressions.
The operand types provide semantic information; they give meaning to the expressions (e.g., "we're comparing two pointers"), and when we look at the constraints on operator==
Originally Posted by
C99 Clause 6.5.9 Paragraph 2
One of the following shall hold:
— both operands have arithmetic type;
— both operands are pointers to qualified or unqualified versions of compatible types;
— one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void; or
— one operand is a pointer and the other is a null pointer constant.
We see that actually the expression has no meaning because the types of the operands of operator== violates the constraints required to interpret it. Therefore, there is a semantic error.
It may be possible to argue that there is also a logical error because the author presumably intended some logic that did not happen because of the semantic error.