13

what is the best way to compare int arrays b and c with a:

int a[] = {0,1,0,0,1};
int b[] = {0,1,0,0,1};
int c[] = {1,1,0,0,1};

b and c are just examples, assume they can be any combination of 0s and 1s.

I am trying to detect arrays identical to a. I have googled this for a while and have not found a satisfactory answer.

This is a beginners question I realise, thank you for your patience.

user1083734
  • 4,815
  • 8
  • 24
  • 24

5 Answers5

32

Use the standard memcmp function from <string.h>.

memcmp(a, b, sizeof(a)) == 0

whenever a and b are equal.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 4
    Minor note: If there are padding bits in the `int` type, `memcmp()` may yield a false negative. Fortunately, the OP is unlikely to run across such implementations. – J. C. Salomon Feb 02 '12 at 21:57
  • @J.C.Salomon: What are "padding bits in the `int` type"? – einpoklum Nov 09 '14 at 15:50
  • Using memcmp in this case in C is not reliable because of possible padding bits. – this Nov 19 '15 at 16:17
  • Except of course that [arrays may not contain padding between elements](http://stackoverflow.com/a/1066719/212858) – Useless Nov 19 '15 at 17:30
  • 1
    @Useless That is true, but it is also a red herring. I was talking about padding bits in integers themselves. – this Nov 19 '15 at 18:06
  • You think there are secret un-initialized bits _inside_ an `int`? Perhaps you could give an actual example of what you mean - in fact, ideally ask (and answer, if you like) a question rather than continuing this in comments. – Useless Nov 20 '15 at 11:59
  • @Useless: Some machines (especially DSPs) have an accumulator whose size is a not a multiple of a memory word [one might have a 16-bit memory word but a 40-bit accumulator]. In some cases, it may be helpful for them to define a type whose useful portion is not a multiple of the word size [e.g. such a platform could define "long" as three bytes long, but with only 40 bits holding useful information]. – supercat Mar 24 '16 at 22:36
5

If you mean

int a[] = {0,1,0,0,1};
int b[] = {0,1,0,0,1};
int c[] = {1,1,0,0,1};

then

memcmp(a, b, sizeof(a)); /* returns zero for a match */
memcmp(a, c, sizeof(a)); /* returns nonzero for no match */
Useless
  • 64,155
  • 6
  • 88
  • 132
4

Use a loop and compare the individual elements one after another.

sth
  • 222,467
  • 53
  • 283
  • 367
  • don't forget to escape the loop when you reach the end of one of the arrays, in case the size is different – Wim Dec 24 '15 at 15:22
0

More information is needed on the question. I can divide your question in two ways as below,

  1. Compare array contents considering order?? Ex:char a[]={a, b, c}, b[]={a, c, b} here since you are considering the order, the contents are not same so a!=b

  1. compare array contents irrespective of order? Ex:char a[]={a, b, c}, b[]={a, c, b} here if you are not considering the order, the contents are same so a==b

Solution for Question no 1: One can use memcmp for this problem. Because memcmp will compare lexicographical and return 0 or 1 or -1 as below

 #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','b','c'};
        char b[]={'a','b','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:0***

    #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','c','b'};
        char b[]={'a','b','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:1***

    #include<stdio.h>
    #include<string.h>
    int main()
    {

        char a[]={'a','b','c'};
        char b[]={'b','a','c'};
        int x=memcmp(a,b,sizeof(a));
        printf("%d\n",x);

    return 0;
    }
***output:-1***

Solution for Question no 2: One can use memcmp for this problem, the best solution for this problem is as below

Here, I answered for the above problem https://stackoverflow.com/a/36130812/5206646

Lax
  • 21
  • 1
  • 9
0

For performance reasons memcmp() will compare byte for byte while int arrays are in 4 byte chunks. This will most likely be pretty slow if the array is large or you have to compare many arrays.

The simplest way is to run a for loop that exits as soon as you find a difference.

bool is_array_equal(const int * a, const int * b, length){
    for (int i = 0; i < length; ++i){
        if(a[i] != b[i]){ return false; }
    }
    return true;
}