This is a strip down of a larger program but shows the problem. I pass an 8 byte array through two subroutines but the sizeof in the last routine reads only 4 bytes, not 8
Can anyone explain please
byte Set_Run[] = {0x01,0x05,0x08,0x14,0xFF,0x00,0xCE,0x5E}; //1,5,8,20,255,0,206,94
/*-----------------------------------------------------------------
'Send an Array To the Controller
'-----------------------------------------------------------------
*/
void Send_To_Controller(byte Buf[])
{
Serial.println("Sending to controller");
SendRTX2(Buf);
}
/*-----------------------------------------------------------------
'Sends to RTX2
'-----------------------------------------------------------------
*/
void SendRTX2(byte Buffer[])
{
Serial.println("Send RTX2");
for (int i=0;i<8;i++)
Serial.println(Buffer[i], HEX);
Serial.print("Buffer Size :");
Serial.println(sizeof(Buffer));
}
void setup()
{
Serial.begin(115200);
Serial.print("Size of Set_Run ");
Serial.println(sizeof(Set_Run));
Send_To_Controller(Set_Run);
}
void loop()
{
}
Here is the printout Size of Set_Run 8 Sending to controller Send RTX2 1 5 8 14 FF 0 CE 5E Buffer Size :4
When you pass array as reference, you lost the info about its size. The size, that you see in the print - is the size of pointer, which always 4byte, regardless array size.
The common workaround for the issue is passing the size of array as separate integer parameter to the function.
If you have access to the Standard Template Library, you may want to have a look at std::span. This container can be used to pass both the content as well as the size of an array to a function.
If this is not the case, you may want to have a look at the array-helpers library, in which a limited version of std::span is implemented.