So if we have:
sometype x;
sometype *a, *b;
a = &x;
b = &x;
then
memcmp(a,b,sizeof(x))
has to return zero?
Didn't you mean
sometype x;
sometype *a, *b;
a = &x;
b = &x;
memcmp(&a, &b, sizeof a);
?
Answering your verbatim question: of course memcmp() has to return zero,
because it compares the representation of object x against the
representation of the same object x.
memcmp(&x, &x, sizeof x);
Answering your question the way I think you've actually meant it: not
necessarily. Identical representations imply equality by "==" (if "==" is
allowed at all in the specific case), but "==" evaluating to 1 doesn't
imply identical representations. Perhaps your (fixed) example above does,
but in general, if you get two objects of type T that are valid at that
point to compare with "==", and "==" evals to 1, memcmp() can still return
nonzero. Or so I seem to remember the standard.
.... See C99 6.2.6 Representations of types, 6.2.6.1 General, paragraph 8:
----v----
Where an operator is applied to a value that has more than one object
representation, which object representation is used shall not affect the
value of the result. 43) Where a value is stored in an object using a type
that has more than one object representation for that value, it is
unspecified which representation is used, but a trap representation shall
not be generated.
----^----
Footnote 43:
----v----
It is possible for objects x and y with the same effective type T to have
the same value when they are accessed as objects of type T, but to have
different values in other contexts. In particular, if == is defined for
type T, then x==y does not imply that memcmp(&x, &y, sizeof (T)) == 0.
Furthermore, x==y does not necessarily imply that x and y have the same
value; other operations on values of type T may distinguish between them.
----^----
Cheers,
lacos