-
Array Problem
What is wrong with the following code segment?
Code:
const int limit = 100;
int eprep[limit];
int examp[limit];
for (int index = 0; index <= limit - 1; index++)
{
eprep[index] = 0;
examp[index] = 0;
}
if (eprep == examp)
cout << "Equal";
At first I thought it was maybe the fact that it has no 'else' but then thought that was too easy of an answer...????
-
Depends on what you mean by "wrong". eprep and examp are not the actual same array in memory, which is what you are checking. eprep and examp may have all the elements the same, but that doesn't mean they are in the same physical place.
-
eprep and examp are pointers to different memory addresses and obviously are not equal.
-
An easy way to test for equality in two arrays is the equal templated function in the <algorithm> header:
Code:
#include <algorithm>
...
if( std::equal(eprep,eprep+limit,examp) )
std::cout << "Equal";
I think that's how you use it... it's been awhile.
Code:
for (int index = 0; index <= limit - 1; index++)
That's the same as (and usually written as):
Code:
for (int index = 0; index < limit; index++)