I am trying to compare two int arrays, element by element, to check for equality. I can’t seem to get this to work. Basic pointer resources also welcome. Thank you!
int *ints;
ints = new int[10];
bool arrayEqual(const Object& obj)
{
bool eql = true;
for(int i=0; i<10; ++i)
{
if(*ints[i] != obj.ints[i])
eql = false;
}
return eql;
}
When you do
if(*ints[i] != obj.ints[i]), what you are comparing is the address pointed byints[i]with the content ofobj.ints[i], instead of the content ofints[i]itself. That is because the name of an array is already a pointer to the first element of an array, and when you add the subscript, you will look for the ith position after the first in that array. That’s why you don’t need the*.The correct is:
Hope I helped!