TheMemberFormerlyKnownAsAWOL:
Just use memcmp; why reinvent the wheel?
Because you cannot use memcmp to meaningfully compare just any two objects. It compares object representations, not object values.
For an array of integers, there's no difference of course, but for comparing structures that contain pointers, padding or other structures with overloaded equality operators, memcmp is useless.
But you're right, there's no reason to reinvent the wheel, just use std::array and compare them using the == operator.
If you're using an AVR, you can use ArduinoSTL (uClib++) or Arduino-Helpers (GCC libc++) for the header.
larryd:
When if( test1 != test2 ) evaluates to Yes (true) then you executed {result = true;}
When if( test1 != test2 ) evaluates to No (false) then you executed {result = false;}
When you remove the else { result = false } code, if ‘result’ is ever set to ‘true’, it will stay ‘true’. (at least until you tell it to change)
Well what I want is to do things the simple way I want to do was to compare a key inside an array with a key entered through the keyboard if it's true returns 1 otherwise it returns 0
TheMemberFormerlyKnownAsAWOL:
Or, thinking in a C mindset, return the difference, so zero if they're the same, non-zero otherwise.
Yes, but in this case is to compare the values of each array whether they are equal or not, if true returns 1 otherwise 0!
I think there is something that I am not quite understanding how arrays are compared
TheMemberFormerlyKnownAsAWOL:
I'm sorry, I missed the point where the question " how to compare two arrays" became "meaningfully compare just any two objects"
I think this is absolutely relevant to the question "how to compare two arrays". Consider this:
When comparing the two arrays, memcmp gives the wrong answer, the == operator for std::array gives the correct answer.
This is a relatively clear-cut case, because String saves its data on the heap, but even something as seemingly simple as comparing arrays of structures invokes undefined behavior, and may randomly give incorrect results due to padding.
TheMemberFormerlyKnownAsAWOL:
But "int" is a fundamental datatype, right?
PieterP:
For an array of integers, there's no difference of course, but for comparing structures that contain pointers, padding or other structures with overloaded equality operators, memcmp is useless.
Yes, int is a fundamental datatype.
OP asked a general question "how to compare arrays". His (clearly synthetic) example code may show an array of integers, but tomorrow he'll want to compare something else, and won't know why it doesn't work.
memcmp is a valid way to compare memory in C, and can be used in certain cases in C++ as well, but should be used with caution.
You're throwing it on, but that's the way it is with these speeches if you learn a lot, sometimes small things big results, too bad I'm not on a higher level