I am using a large struct with many members, and I would like an easy way to quickly see if any of the members are non-zero. I know that memcmp()
should not be used to compare two structs for equality (as explained here: How do you compare structs for equality in C?), but I am hoping that by comparing a struct to a block of memory that has been set to 0, this may work.
For example, assume I have some struct:
typedef struct {
int Int1;
int Int2;
int Int3;
} MyInts;
I create a variable from this struct:
MyInts MyStruct = {0};
Throughout execution of my program, the members of MyStruct
will usually be 0, but may occasionally be set to some non-zero value temporarily. I want to quickly check if any of the ints in MyStruct
are non-zero. Can I use memcmp()
as below?
// Create empty struct to test against
MyInts EmptyStruct = {0};
// Make sure entire memory block of test struct is cleared
memset(&EmptyStruct, 0, sizeof(MyInts));
// Compare MyStruct to EmptyStruct to see if any non-zero members exist
int result = memcmp(&MyStruct, &EmptyStruct, sizeof(MyInts));