The C language supports three operations on a struct (i.e. a record): accessing its members, taking its address, and copying it as a unit. The following is an attempt to add a forth operation, compare,for testing equality of two structs of the same type:
int compare(char *p1, char *p2, int size)
{
while (size-->0)
if (*(p1++) != *(p2++)) return 0;
return 1;
}
struct my_record { ... } r1, r2;
r1 = ...;
r2 = ...;
if (compare((char *)&r1, (char *)&r2, sizeof(struct my_record)))
printf("r1 == r2n");
else
printf("r1 != r2n");
What is the idea behind this approach? Does it always work? Why or why not?