Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equality of arrays In c

Tags:

arrays

c

I have to check equality of two arrays(1-D) with integer elements.

I understand that there is no direct comparison way. So i'm doing basic iteration and checking for equality of each element.

 for ( int i = 0 ; i < len ; i++) {
    // Equality check

What is the most efficient way to test equality of arrays in C ? Can i get away with loops(for ..) somehow ?

like image 944
user2754673 Avatar asked Feb 08 '23 04:02

user2754673


1 Answers

Use memcmp function to compare two arrays of equal length.

int a = memcmp(arr1, arr2, sizeof(arr1));
if(!a)
    printf("Arrays are equal\n");
else
    printf("Arrays are not equal\n");
like image 132
haccks Avatar answered Feb 21 '23 04:02

haccks