Sjouke Burry wrote:
Yong Jing wrote:
I have a struct of over 50 members
struct A {
type -- 50 members
A(){initialize all members}
}B[2];
How can I compare B[0] with B[1], please?
Regards,
Yong jin (~.~E)
memcmp(&B[0],&B[1],sizeof(B[0]))
That will tell you if the respresentations of the two structures is
identical, including any padding, which is almost certainly not what
the OP is trying to accomplish. If memcmp returns 0 then you know the
two strutures are equal, otherwise you don't know anything, not
generally useful. This is because structures may contain padding which
does not contribute to the values of its members, this padding may be
different without affecting the values of the members. Even if you
knew that there was no padding, you wouldn't use memcmp to test for
equality for the same reason you wouldn't do this for any other type:
most types are allowed to have padding bits that don't contribute to
their value or multiple representations for the same values. If the
structures being compared contain any such types, they may not compare
equal with memcmp when their members have equivalent values. There is
a good reason that C does not allow the comparision of structures. The
solution is to write a function that compares the values of each member
of the structures.
Robert Gamble