Calculate Array Size by Function Results in Error

void setup()
{
  Serial.begin(115200);
  byte hexValues[] = {0xAB, 0xBA, 0x11, 0x15, 0x12, 0x15};
  Serial.println(sizeof(hexValues) / sizeof(byte)); //Produces 6, correct result
  Serial.println(calculateArraySize(hexValues)); //Always produces 2 despite size of array, incorrect result
}

void loop() 
{

}

byte calculateArraySize(byte arr[])
{
  return sizeof(arr) / sizeof(byte);
}

Could someone help me understand why, when passing the calculation of an array size through a function, results in the wrong value?

Because you’re not passing the array to the function, only a pointer to it.

This has been true in C/C++ since the 1970s

Got it, thanks for pointing me in the right direction. Did a little researching and seems that I am out out luck passing the array through a function.

Note too that

Serial.println(sizeof(hexValues) / sizeof(byte));

only works for an array of bytes and because sizeof(byte) will always be 1 you don't need to use it

It is better to use

Serial.println(sizeof(arrayName) / sizeof(arrayName[0]));

which will return the number of elements in an array of any data type

Byork:
Got it, thanks for pointing me in the right direction. Did a little researching and seems that I am out out luck passing the array through a function.

If you are comfortable with template:

template<size_t N> byte calculateArraySize(byte (&arr)[N])
{
  return sizeof(arr) / sizeof(byte);
  //Also same as return N;
}

Otherwise, just pass the size of the array along with the pointer.