Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to compare arrays in C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

int compareArrays(int arr1[], int arr2[], int size1, int size2) {
    if (size1 != size2) {
        return 0;
    }
    
    //  Check if arrays are exactly equal (same values & order)
    return memcmp(arr1, arr2, size1) == 0;
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {1, 2, 3, 4, 5};
    
    int size1 = sizeof(arr1) / sizeof(arr1[0]);
    int size2 = sizeof(arr2) / sizeof(arr2[0]);
    
    if (compareArrays(arr1, arr2, size1, size2)) {
        printf("arr1 == arr2");
    } else {
        printf("arr1 != arr2");
    }

    return 0;
}

 
 
/*
run:
 
arr1 == arr2
 
*/

 



answered Dec 27, 2020 by avibootz
edited Nov 13, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

// Check if arrays have the same values regardless of order

// Comparison function for qsort
int compare_ints(const void *a, const void *b) {
    int int_a = *(const int *)a;
    int int_b = *(const int *)b;
    
    return (int_a > int_b) - (int_a < int_b);
}

// Function to check if two arrays have the same elements regardless of order
bool have_same_values(int arr1[], int arr2[], size_t size) {
    if (size == 0) return true; // Empty arrays are considered equal

    // Sort both arrays
    qsort(arr1, size, sizeof(int), compare_ints);
    qsort(arr2, size, sizeof(int), compare_ints);

    //  Check if arrays are exactly equal (same values & order)
    return memcmp(arr1, arr2, size) == 0;
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {4, 3, 5, 2, 1};
    size_t size1 = sizeof(arr1) / sizeof(arr1[0]);
    size_t size2 = sizeof(arr2) / sizeof(arr2[0]);

    // Validate sizes first
    if (size1 != size2) {
        printf("Arrays are NOT equal (different sizes).\n");
        return 0;
    }

    if (have_same_values(arr1, arr2, size1)) {
        printf("Arrays have the SAME values regardless of order.\n");
    } else {
        printf("Arrays do NOT have the same values.\n");
    }

    return 0;
}

  
  
/*
run:
  
Arrays have the SAME values regardless of order.
  
*/

 



answered Nov 14, 2025 by avibootz

Related questions

1 answer 41 views
2 answers 268 views
1 answer 118 views
118 views asked Jun 5, 2023 by avibootz
2 answers 142 views
2 answers 109 views
3 answers 185 views
185 views asked Jan 25, 2022 by avibootz
3 answers 223 views
...