# Thread: How do I compare two arrays?

1. ## How do I compare two arrays?

I'm a bit new to programming in c and have come across the problem of trying to compare two two-dimensional arrays (let's say 10 by 10).

Could I write for instance

if(array1==array2)
printf("the arrays are the same");

or do I have to write
if(array1[10][10]==array2[10][10]")
printf("the arrays are the same");

?

I also would like to figure out, (if anyone can help me) how to figure out if an array is all full of zeroes, i.e. how to figure out if it's empty.

Any help would be appreciated. Thank you.

2. You'll have to loop through and look at every value in the array.

3. no you can use the strcmp from the <string.h> library.

so if strcmp( array1, array2 ) == 0
then they are equal
if strcmp( array1, array2 ) == 1
then array1 is bigger then array2
if strcmp( array1, array2) == -1
then array1 is less then array2

4. I got it to work. Thanks a lot.

5. >>Could I write for instance

>>if(array1==array2)
>>printf("the arrays are the same");

This will not work. You have to run through with for loops to look at each element, i suggest using 1 variable set to 0, and if atleast 1 of the elements are not equal, then set it to 1, you can also use a break statement to save time by gettingout of the for loops. Then if its 1 at the end, its not equal, if its 0, it is equal.

[/edit]

6. to find out if the array is full of zeroes you could loop through the array and create a flag.
initialise this flag to false to begin with and the go through the array and see if the value is zero. if it is make the flag true, cycle through and the moment you hit a non-zero value then you should break out of the loop and return the value of the flag which would be false. if the flag remains true all through the loop then its full of zeroes.

7. Originally posted by kurz7
no you can use the strcmp from the <string.h> library.
This only works if they are character arrays. It won't work for any other data type. (ie: float, int, struct, etc)

Just a simple loop will suffice.

Quzah.