Compare 2 Arrays [Wemos D1 Mini]


I`m building a network using Wemos D1 Mini and i need to compare some address 2 by 2 inside an array.
I tough that this should be an easy task, but...

I want to use a function called "CompareMAC" to simplify the code, but look like that i cannot pass an array bigger than 4 elements.

This is my code:

uint8_t mac_peer1[] = {0x8C, 0xAA, 0xB5, 0x78, 0xD2, 0x4B};  
uint8_t mac_peer2[] = {0x8C, 0xAA, 0xB5, 0x78, 0xFF};  

void setup(void) {
  Serial.print("\nMAC 1: "+String(mac_peer1[0],HEX)); 
  for (int i =1; i<sizeof(mac_peer1); i++) Serial.print(":"+String(mac_peer1[i],HEX)); 
  Serial.print("\nMAC 2: "+String(mac_peer2[0],HEX)); 
  for (int i =1; i<sizeof(mac_peer2); i++) Serial.print(":"+String(mac_peer2[i],HEX)); 
  if (CompareMAC (mac_peer1, mac_peer2) == 1) Serial.println ("\nOK");
  else Serial.println ("\nDifferent MAC");

void loop(void) {

//////////========== COMPARE MAC ARRAY ==========//////////
int CompareMAC (uint8_t a[], uint8_t b[]){
  Serial.print("\nMAC A: "+String(a[0],HEX)); 
  for (int i =1; i<sizeof(a); i++) Serial.print(":"+String(a[i],HEX)); 
  Serial.print("\nMAC B: "+String(mac_peer2[0],HEX)); 
  for (int i =1; i<sizeof(b); i++) Serial.print(":"+String(b[i],HEX)); 
  if (sizeof(a)!=sizeof(b)) {
    Serial.println("\nCannot compare the Arrays");
    return 0;
  for (int i=0; i<sizeof(a); i++) {
    if (a[i] != b[i]) return 0;
  return 1;

An this is what i get:

17:53:18.859 -> 
17:53:18.859 -> MAC 1: 8c:aa:b5:78:d2:4b
17:53:18.859 -> MAC 2: 8c:aa:b5:78:11
17:53:18.859 -> MAC A: 8c:aa:b5:78
17:53:18.859 -> MAC B: 8c:aa:b5:78
17:53:18.859 -> OK

Some one can help me to understand what is happening?

Thank you!

When you pass an array to a function what you are actually passing is a pointer to the first element of the array, which is an int, so when you use sizeof() what you are getting is the size of an int on your system, which is 4 bytes

Either pass the array size to the function or use a fixed value if that is appropriate

1 Like

Also, use memcmp

1 Like

Thank you very much to both!

I have flag UKHeliBob as "solution" since it is the direct reply to my question, but also using memcmp work very well!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.