0

Is there a good way (or function) to compare all elements of two arrays (or selected elements) without hardcoding (i.e., while(a[1] == b[1] && a[2] == b[2] && ...)) and without using a loop?

1
  • I edited the question and replaced 'for loop' with only 'loop' else the obvious answer to your question would be while or do-while loop then.
    – sinister
    Aug 9 '14 at 13:32
4

Have a look at memcmp()!

Also, you can define your own function

bool compare(datatype array1[], datatype array2[], size_t size1, size_t size2)
{
    // check whether they're both of the same size
    if (size1 != size2)
    {
        return false;
    }

    // compare
    for (int i = 0; i < size1; i++)
    {
        if (array1[i] != array2[i])
        {
            return false;
        }
    }

    return true;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .