Comparing arrays for equality in C++

C++ArraysComparisonEquality

C++ Problem Overview


Can someone please explain to me why the output from the following code is saying that arrays are not equal?

int main()
{

	int iar1[] = {1,2,3,4,5};
	int iar2[] = {1,2,3,4,5};
	
	if (iar1 == iar2)
		cout << "Arrays are equal.";
	else
		cout << "Arrays are not equal.";

	return 0;	
}

C++ Solutions


Solution 1 - C++

if (iar1 == iar2)

Here iar1 and iar2 are decaying to pointers to the first elements of the respective arrays. Since they are two distinct arrays, the pointer values are, of course, different and your comparison tests not equal.

To do an element-wise comparison, you must either write a loop; or use std::array instead

std::array<int, 5> iar1 {1,2,3,4,5};
std::array<int, 5> iar2 {1,2,3,4,5};

if( iar1 == iar2 ) {
  // arrays contents are the same

} else {
  // not the same

}

Solution 2 - C++

Since nobody mentioned it yet, you can compare arrays with the std::equal algorithm:

int iar1[] = {1,2,3,4,5};
int iar2[] = {1,2,3,4,5};

if (std::equal(std::begin(iar1), std::end(iar1), std::begin(iar2)))
    cout << "Arrays are equal.";
else
    cout << "Arrays are not equal.";

You need to include <algorithm> and <iterator>. If you don't use C++11 yet, you can write:

if (std::equal(iar1, iar1 + sizeof iar1 / sizeof *iar1, iar2))

Solution 3 - C++

You're not comparing the contents of the arrays, you're comparing the addresses of the arrays. Since they're two separate arrays, they have different addresses.

Avoid this problem by using higher-level containers, such as std::vector, std::deque, or std::array.

Solution 4 - C++

Array is not a primitive type, and the arrays belong to different addresses in the C++ memory.

Solution 5 - C++

Nobody mentions memcmp? This is also a good choice.

/* memcmp example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=memcmp ( buffer1, buffer2, sizeof(buffer1) );

  if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}

Ref: http://www.cplusplus.com/reference/cstring/memcmp/

Solution 6 - C++

If you are reluctant to change your existing code to std::array, then use a couple of methods instead which takes non-type template arguments :

//Passed arrays store different data types
template <typename T, typename U, int size1, int size2>
bool equal(T (&arr1)[size1], U (&arr2)[size2] ){
	return false;
}
 
//Passed arrays store SAME data types
template <typename T, int size1, int size2>
bool equal(T (&arr1)[size1], T (&arr2)[size2] ){
	if(size1 == size2) {
		for(int i = 0 ; i < size1; ++i){
			if(arr1[i] != arr2[i]) return false;
		}
		return true;
	}
	return false;
}

Here is the demo. Note that, while calling, we just need to pass the array variables e.g. equal(iar1, iar2) in your case, no need to pass the size of arrays.

Solution 7 - C++

You are comparing the addresses instead of the values.

Solution 8 - C++

Both store memory addresses to the first elements of two different arrays. These addresses can't be equal hence the output.

Solution 9 - C++

If you are willing to use std::array instead of built-in arrays, you may use:

std::array<int, 5> iar1 = {1,2,3,4,5};
std::array<int, 5> iar2 = {1,2,3,4,5};

if (iar1 == iar2)

Solution 10 - C++

Right. In most, if not all implementations of C, the array identifier can be implicitly casted to a pointer to the first element (i.e. the first element's address). What you're doing here is comparing those addresses, which is obviously wrong.

Instead, you need to iterate over both arrays, checking each element against each other. If you get to the end of both without a failure, they're equal.

Solution 11 - C++

When we use an array, we are really using a pointer to the first element in the array. Hence, this condition if( iar1 == iar2 ) actually compares two addresses. Those pointers do not address the same object.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvladinkocView Question on Stackoverflow
Solution 1 - C++PraetorianView Answer on Stackoverflow
Solution 2 - C++InstallingXubuntuRightNowView Answer on Stackoverflow
Solution 3 - C++Fred LarsonView Answer on Stackoverflow
Solution 4 - C++Paul S.View Answer on Stackoverflow
Solution 5 - C++Xiangyi MengView Answer on Stackoverflow
Solution 6 - C++Saurav SahuView Answer on Stackoverflow
Solution 7 - C++Rahul TripathiView Answer on Stackoverflow
Solution 8 - C++Vachaspati MishraView Answer on Stackoverflow
Solution 9 - C++R SahuView Answer on Stackoverflow
Solution 10 - C++slugonamissionView Answer on Stackoverflow
Solution 11 - C++vladinkocView Answer on Stackoverflow