-1

I am having a byte array let us say

byte arr[7]={123456};

How to compare this array with a list of of other array like this for example

byte  buffers[5][7] = {123456,654321,987654,456789,765432};

if this method is wrong how can I declare that ?

  • 1
    what do you think byte arr[7]={123456}; actually does? – Jaromanda X Feb 24 '19 at 3:58
  • so how can i make a list of arrays to compare with ? – Amr Ahmed Feb 24 '19 at 4:01
  • 1
    well, you're doing it wrong – Jaromanda X Feb 24 '19 at 4:10
  • 1
    to learn how to initialise an array - see answer – Jaromanda X Feb 24 '19 at 4:11
  • 1
    How is this an Arduino question? This is more a "help me understand basic C/C++ programming"-question. – Mikael Patel Feb 24 '19 at 10:42
2
byte arr[7]={123456};

sets arr[0] = 64 - which is 123456 % 256 - and arr[1-6] are left at 0

And you also only want 6 (not 7) values - so the code would be

byte arr[6]={1,2,3,4,5,6}; // this is how to initialise an array
byte  buffers[5][6] = {{1,2,3,4,5,6},{6,5,4,3,2,1},{9,8,7,6,5,4},{4,5,6,7,8,9},{7,6,5,4,3,2}}; // this is how to initialise a 2d array

void setup() {
  Serial.begin(74880);
  delay(100);
  // this code will find a match, the vlaue of `i` will be the matched index
  int i;
  int j;
  for(i = 0; i < 5; i++) {
    for(j = 0; j < 6; j ++) {
      if(arr[j] != buffers[i][j]) {
        break;
      }
    }
    if(j == 6) {
      break;
    }
  }
  if (i < 5) {
    Serial.print("Found at index ");
    Serial.println(i);
  }

}
| improve this answer | |
  • 1
    @JaromandaX Might want to make this an Arduino QA by adding how to use program memory for a constant value vector/array. – Mikael Patel Feb 24 '19 at 10:41
  • not sure what you mean @MikaelPatel – Jaromanda X Feb 25 '19 at 0:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.