How to compare two arrays

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)

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.

[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Arduino_Helpers[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]AH[/color][/b][color=#434f54]/[/color][color=#000000]STL[/color][color=#434f54]/[/color][color=#00979c]array[/color][color=#434f54]>[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]int[/color][color=#434f54],[/color] [color=#000000]6[/color][color=#434f54]>[/color] [color=#000000]a[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]2[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#000000]}[/color][color=#000000];[/color]
  [color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]int[/color][color=#434f54],[/color] [color=#000000]6[/color][color=#434f54]>[/color] [color=#000000]b[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#000000]0[/color][color=#434f54],[/color] [color=#000000]1[/color][color=#434f54],[/color] [color=#000000]2[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#434f54],[/color] [color=#000000]5[/color][color=#000000]}[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000]115200[/color][color=#000000])[/color][color=#000000];[/color]
  [color=#5e6d03]while[/color] [color=#000000]([/color][color=#434f54]![/color][b][color=#d35400]Serial[/color][/b][color=#000000])[/color][color=#000000];[/color]
  [b][color=#d35400]Serial[/color][/b][color=#434f54].[/color][color=#d35400]println[/color][color=#000000]([/color][color=#000000]a[/color] [color=#434f54]==[/color] [color=#000000]b[/color] [color=#434f54]?[/color] [color=#005c5f]"equal"[/color] [color=#434f54]:[/color] [color=#005c5f]"not equal"[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color][color=#000000]}[/color]

Fiasgardone:
yes, but it is always good to know all the features offered by the C language.

Arduino uses C++, not C.

Pieter

I'm sorry, I missed the point where the question " how to compare two arrays" became "meaningfully compare just any two objects"

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

Or, thinking in a C mindset, return the difference, so zero if they're the same, non-zero otherwise.

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

If memcmp returns zero if the arrays are identical, it is very simple (if a trifle misguided) to convert that zero to "true".

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:

  [color=#00979c]String[/color] [color=#000000]a1[/color][color=#000000][[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#005c5f]"one"[/color][color=#434f54],[/color] [color=#005c5f]"two"[/color][color=#434f54],[/color] [color=#005c5f]"three"[/color][color=#000000]}[/color][color=#000000];[/color]
  [color=#00979c]String[/color] [color=#000000]b1[/color][color=#000000][[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#005c5f]"one"[/color][color=#434f54],[/color] [color=#005c5f]"two"[/color][color=#434f54],[/color] [color=#005c5f]"three"[/color][color=#000000]}[/color][color=#000000];[/color]
  [color=#d35400]memcmp[/color][color=#000000]([/color][color=#000000]a1[/color][color=#434f54],[/color] [color=#000000]b1[/color][color=#434f54],[/color] [color=#00979c]sizeof[/color][color=#000000]([/color][color=#000000]a1[/color][color=#000000])[/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// not equal[/color]

  [color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]String[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]a2[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#005c5f]"one"[/color][color=#434f54],[/color] [color=#005c5f]"two"[/color][color=#434f54],[/color] [color=#005c5f]"three"[/color][color=#000000]}[/color][color=#000000];[/color]  
  [color=#000000]std[/color][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]array[/color][color=#434f54]<[/color][color=#00979c]String[/color][color=#434f54],[/color] [color=#000000]3[/color][color=#434f54]>[/color] [color=#000000]b2[/color] [color=#434f54]=[/color] [color=#000000]{[/color][color=#005c5f]"one"[/color][color=#434f54],[/color] [color=#005c5f]"two"[/color][color=#434f54],[/color] [color=#005c5f]"three"[/color][color=#000000]}[/color][color=#000000];[/color]
  [color=#000000]a2[/color] [color=#434f54]==[/color] [color=#000000]b2[/color][color=#000000];[/color]                   [color=#434f54]// equal[/color]

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.

But who the Hell uses Strings?

TheMemberFormerlyKnownAsAWOL:
But who the Hell uses Strings?

It's not about Strings. It's about literally anything that's not a fundamental data type.

But "int" is a fundamental datatype, right?

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.

Again, I missed the "clearly synthetic" catch, but thanks for the heads-up.

I don't like it when Mom and Dad fight. :cry:

larryd:
I don't like it when Mom and Dad fight. :cry:

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