The reason behind the fact that the structures can't be checked for equality in C is the presence of slack bytes,which makes the comparison impossible.
But if I use #pragma pack(1) which removes the slack bytes then the comparison should be done smoothly,but it still gives error on being compared.
Example Code
#include<stdio.h>
#pragma pack(1)
struct person
{
int uid;
char nameStart;
};
struct personDupe
{
int uid;
char nameStart;
};
int main()
{
struct person var;
struct personDupe varDupe;
printf("\nSize of person : %3d\n",sizeof(var));
printf("\nSize of personDupe : %3d\n",sizeof(varDupe));
var.uid = 12;
var.nameStart = 'a';
varDupe.uid = 12;
varDupe.nameStart = 'a';
if(var == varDupe) //Error is introduced
printf("\nStructures are equal\n");
return 0;
}
You can also tell the compiler how you detect that two values are the same
bool same_person(struct person* p, struct personDupe* dupe)
{ return p->uid == dupe->uid && p->nameStart == dupe->nameStart; }
And then you can do
if(same_person(&var, &varDupe))
printf("\nStructures are equal\n");