简体   繁体   中英

How can I compare two 2D arrays in C?

İ try it, but this code not run stable.

İ have two 2D arrays first[a][b] and second[x][y] .

I want to compare these two arrays. How can I do this?

My code:

    for (int i = 0; i < 10; i++) 
        for (int j = 0; j < 10; j++)
            if (first[i][j] == second[i][j])
                return 1;

An alternative way would be using memcmp .

NOTE: This will work on modern architectures, where int does not have any padding bits. Thanks for @chqrlieforyellowblockquotes to pointing this out.

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

int main() {
  int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  int b[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  int c[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 123}};
  int ret;

  ret = memcmp(a, b, 3 * 3 * sizeof(int));

  if (ret == 0)
    printf("a equal b\n");
  else
    printf("a not equal b\n");

  ret = memcmp(a, c, 3 * 3 * sizeof(int));

  if (ret == 0)
    printf("a equal c\n");
  else
    printf("a not equal c\n");

  return 0;
}

Output

a equal b
a not equal c

Through your code, I see that:

both the arrays are equal if one of the corresponding elements in the arrays are equal.

But I think you want this:

both the arrays are equal if all the corresponding elements in the arrays are equal

That does not happen in your code because you are return ing immediately when one of the corresponding elements are equal ( == ), so the remaining checks are omitted by your code.

Solution:

Change if( first[i][j]==second[i][j]) to if (first[i][j] != second[i][j])

This will look for the first unequal element. Then you can return the appropriate message or perhaps break depending on what exactly you are doing with that code.

You were on the right track, but the arrays are identical if all entries are. You should return 0 when you find a difference and return 1 otherwise:

int compare_matrices(int first[10][10], int second[10][10]) {
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (first[i][j] != second[i][j])
                return 0;
       }
    }
    return 1;
}

An alternative for regular architectures without padding bits would use memcmp() :

#include <string.h>

int compare_matrices(int first[10][10], int second[10][10]) {
    return !memcmp(first, second, 10 * sizeof(*first));
}

The first code is simplistic but the compiler can optimize it by unrolling the loops and may produce performance comparable or better than that of memcmp() .

Reducing the number of tests would increase the number of memory accesses but may produce better performance as the compiler can use SIMD instructions:

int compare_matrices(int first[10][10], int second[10][10]) {
    for (int i = 0; i < 10; i++) {
        int res = 0;
        for (int j = 0; j < 10; j++) {
            res |= (first[i][j] != second[i][j]);
        }
        if (res) return 0;
    }
    return 1;
}

You can use GodBolt's Compiler Explorer to compare code generators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM