OneBite.Dev - Coding blog in a bite size

check if two arrays are equal in C

Code snippet on how to check if two arrays are equal in C

  bool isEqual(int arr1[], int arr2[], int n) 
{ 
    // Compare all values 
    for (int i = 0; i < n; i++) 
        if (arr1[i] != arr2[i]) 
            return false; 
  
    return true; 
} 

This code checks to see if two arrays are equal. The two parameters of the function are two arrays (arr1 and arr2) and the amount of elements in each array (n). The function iterates through the two arrays one element at a time, comparing each one. If any of the elements are different, the function returns false. If all elements in both arrays are equal, the function returns true. The loop not only checks for differences between elements but also that both arrays have the same size.

c