The below sample caught my attention i.e
int a ;
unsigned int b;
int func()
{
return a =b;
}
the compiler didn't warn me about the signed mismatch in the above case.
where as
int *a ;
unsigned int *b;
int func()
{
a =b;
return *a;
}
compiler warns me as
warning: pointer targets in assignment differ in signedness [-Wpointer-sign]
I’m bit confused or i'm missing something here .
In most cases, the language standard defines a construct in terms of
syntax, constraints and semantics. A diagnostic is required in the case
of a constraint violation, but a compiler may also warn about other
things if it likes.
The first case meets all the constraints (so no diagnostic is required)
and the assignment proceeds by converting the value of the right operand
to the type of the left operand. This conversion is not entirely safe,
but the compiler is not obliged to tell you about it. You may, however,
be able to ask for such a warning. For example, if you are using gcc or
clang, you can do so by adding -Wsign-conversion to the compile command.
In the second case, the assignment does not meet the constraints, so a
diagnostic *is* required. The description of the constraints is not
easy to follow[1] but the gist of it that the two pointers should be
pointers to compatible types, but 'int' and 'unsigned int' are not. The
rules for what constitute compatible types are rather detailed, but to a
first approximation, 'compatible type' means 'the same type'.
[1] Here are the C11 constraints specific to simple assignments:
-- the left operand has atomic, qualified, or unqualified arithmetic
type, and the right has arithmetic type;
-- the left operand has an atomic, qualified, or unqualified version of
a structure or union type compatible with the type of the right;
-- the left operand has atomic, qualified, or unqualified pointer type,
and (considering the type the left operand would have after lvalue
conversion) both operands are pointers to qualified or unqualified
versions of compatible types, and the type pointed to by the left has
all the qualifiers of the type pointed to by the right;
-- the left operand has atomic, qualified, or unqualified pointer type,
and (considering the type the left operand would have after lvalue
conversion) one operand is a pointer to an object type, and the other
is a pointer to a qualified or unqualified version of void, and the
type pointed to by the left has all the qualifiers of the type
pointed to by the right;
-- the left operand is an atomic, qualified, or unqualified pointer, and
the right is a null pointer constant; or
-- the left operand has type atomic, qualified, or unqualified _Bool,
and the right is a pointer.