I have two data structures:
typedef struct{
int a;
int b;
int c;
}EVENTS;
EVENTS typeone[20];
EVENTS typetwo[20];
These have been filled. typeone has been filled till typeone[5] and typetwo till typetwo[8].
I just want to compare the first six of typeone and typetwo and see if there are equal in all their members.
Is there a way to do typeone[1] == typetwo[1]
Basically comparing all the values inside the datastructure at [1].
Is there a short way to do this or would I have to loop through each member and compare separately?
Thanks
Note
#pragma pack(1). It ensures that there are no padding bytes in the structures. This way memcmp will not try to compare padding bytes and the comparison is way faster than a field-by-field method, but while in this case the performance is unlikely to be adversely affected, take:Retrieving
foo.bwill take much more machine code than in case of padded structures, because it will miss word-aligned position where it can be retrieved with a single 32-bit instruction, it will have to be picked out with four byte-reads, and then assembled into the target register from these four pieces. So, take the performance impact into account.Also, check if your compiler supports
#pragma pack. Most modern compilers do, but exceptions may still happen.