4

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 ?

user2754673
  • 439
  • 3
  • 13

3 Answers3

5

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");
haccks
  • 104,019
  • 25
  • 176
  • 264
2

As others have said, using memcmp() is effective.

A general answer, assuming actual arrays, is

int is_equal = sizeof(array1) == sizeof(array2) && !memcmp(array1, array2, sizeof(array1));

If the arrays are supplied as pointer arguments to a function, the size information is lost, and needs to be provided separately.

 int IsEqual(void *array1, void *array2, size_t size1, size_t size2)
 {
     return size1 == size2 && !memcmp(array1, array2, size1);
 }

 int main()
 {
      int arr1[] = { /* whatever */ };
      int arr2[] = { /* whatever */ };

      is_equal = IsEqual(arr1, arr2, sizeof(arr1), sizeof(arr2));
      return 0;
 }

or, preserve type information (i.e. knowledge of working with an array of int) as late as possible before converting to void pointers, and work with number of elements.

 int IsEqual2(int array1[], int array2[], size_t n1, size_t n2)
 {
      /*  n1 and n2 are number of ints in array1 and array2 respectively */
     return n1 == n2 && !memcmp(array1, array2, n1 * sizeof(int));
 }

 int main()
 {
      int arr1[] = { /* whatever */ };
      int arr2[] = { /* whatever */ };

      is_equal = IsEqual2(arr1, arr2, sizeof(arr1)/sizeof(*arr1), sizeof(arr2)/sizeof(*arr2));
      return 0;
 }
Peter
  • 35,646
  • 4
  • 32
  • 74
1

You can refer for loop method

Or else memcmp is also an efficient way of comparing array. Refer memcmp

int memcmp(const void *s1, const void *s2, size_t n);

You can compare first n byte with this function

int array1[5],array2[5]; 
int x = memcmp(array1, array2, sizeof(array1));

If array size is different then:

  • sizeof(array1) is less than sizeof(array2) ,it returns negative value.
  • sizeof(array1) is greater than sizeof(array2) ,it returns positive value.
  • sizeof(array1) is equal to sizeof(array2), it returns zero.

    How to use memcmp ? Answer is also on SO.

REMEMBER memcmp is only useful when both memory element or arraysize are equal

Community
  • 1
  • 1
Punit Vara
  • 3,744
  • 1
  • 16
  • 30
  • 1
    But I'm using equality check as a constraint so the result has to come with an assignment that makes sure each array elemet was equal hence short circuiting trick shown won't be much of my use. Thanks punit. – user2754673 Dec 28 '15 at 04:45
  • 1
    @user2754673 There is no "short circuiting trick" here. It's a perfectly valid answer that takes into account that your question is largely a duplicate of another question. The only flaw with this answer is it suffers the same limitation that haccks' answer does (are the arrays equal size?). – Carey Gregory Dec 28 '15 at 05:02
  • 1
    @punitvara Please explain the comment you added about bit padding. There should be nothing about C arrays that would make `memcmp` unreliable as long as the two arrays are of the same type. – Carey Gregory Dec 28 '15 at 05:11
  • 1
    I hope now I have cleared all the doubts of the questioner :-) – Punit Vara Dec 28 '15 at 05:15
  • Note that with sizeof operator used like this, array1 must be of array type, and not a pointer (for example function parameter). – hyde Dec 28 '15 at 05:25