How would I go about checking the equality of two arrays using memcmp?
bool array_is_equal(const void *array_one, void *array_two, const size_t elem_size, const size_t elem_count)
I have something like this:
int i;
for(i = 0; i < elem_count; i++){
if(memcmp(array_one, array_two, elem_size) == 0) {
return true;
}
i++;
}
return false;
with elem_size being the number of bytes each array element uses in array one
If all you care about is binary equality of two array objects, then you don't even need a cycle
bool array_is_equal(const void *array_one, void *array_two,
const size_t elem_size, const size_t elem_count)
{
return memcmp(array_one, array_two, elem_count * elem_size) == 0;
}
It is not clear though why array array_two
is suddenly non-const (while array array_one
is const). And there's not much point in declaring elem_size
and elem_count
as const (aside from fairly cosmetic considerations).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With