0

I'm having trouble with some code that I'm writing using the Arduino IDE.

The problem I'm having occurs when I try to pass an array of type uint8_t of length 12 to a function. When passing the array, it appears to be reduced in size (from 12 to 4).

I can't figure out what is going on here. Any help would be greatly appreciated!

Here is my code:

int Serialbaud=19200;
int byteCount;
uint8_t message[] = {0x06, 0x01, 0x08, 0x01, 0xF0, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01};

// Test the transmission of our message (uint8_t array)
void testFunc(uint8_t *MSG) {

  uint32_t len= sizeof(MSG)/sizeof(uint8_t);

  Serial.println();
  Serial.println("After passing to function the message is " + String(len) + " bytes:");
  for(int i=0; i<len; i++) {
    Serial.print(MSG[i]);
    Serial.print(", ");
  }
  Serial.println();

}//end function

//--------SETUP------------------
void setup()
{
 delay(3000);//Give yourself time to open up the serial monitor 
 Serial.begin(Serialbaud);  //Begin serial ommunication with Serial Monitor

 //Report the original length and content of the message to the serial monitor
 uint32_t len= sizeof(message)/sizeof(uint8_t); 
 Serial.println("Original message before passing to function is " + String(len) + " bytes:");
 for(int i=0; i<len; i++) {
    Serial.print(message[i]);
    Serial.print(", ");
  }
  Serial.println();

 //Pass the message to the test function
 testFunc(message);

}

//--------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP--
void loop()
{

}//END LOOP-------------------

Here is what the serial monitor output looks like:

Original message before passing to function is 12 bytes: 6, 1, 8, 1, 240, 1, 1, 1, 0, 0, 0, 1,

After passing to function the message is 4 bytes: 6, 1, 8, 1,

macdonaldtomw
  • 165
  • 3
  • 9

2 Answers2

1

You cannot pass arrays by value in C, as arrays decay to pointers to the irst element in nearly every case.
Also, unless you pass the information along or it is part of the contract, you cannot know how long the array pointed to by any function argument was in the caller, which is useless information anyway. Instead, pass an argument saying how many elements should be processed.

You get sizeof MSG == 4, because on your platform such pointers are 4 byte long.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
0

You have to pass vector size to your function (you can not read vector size from a pointer MSG, in fact in your case sizeof(MSG) is equal to sizeof(uint8_t*) and it depends by your platform ), so you could change the function as follow:

int Serialbaud=19200;
int byteCount;
uint8_t message[] = {0x06, 0x01, 0x08, 0x01, 0xF0, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01};

// Test the transmission of our message (uint8_t array)
void testFunc(uint8_t *MSG, int len) {

  //uint32_t len= sizeof(MSG)/sizeof(uint8_t);

  Serial.println();
  Serial.println("After passing to function the message is " + String(len) + " bytes:");
  for(int i=0; i<len; i++) {
    Serial.print(MSG[i]);
    Serial.print(", ");
  }
  Serial.println();

}//end function

//--------SETUP------------------
void setup()
{
 delay(3000);//Give yourself time to open up the serial monitor 
 Serial.begin(Serialbaud);  //Begin serial ommunication with Serial Monitor

 //Report the original length and content of the message to the serial monitor
 uint32_t len= sizeof(message)/sizeof(uint8_t); 
 Serial.println("Original message before passing to function is " + String(len) + " bytes:");
 for(int i=0; i<len; i++) {
    Serial.print(message[i]);
    Serial.print(", ");
  }
  Serial.println();

 //Pass the message to the test function
 testFunc(message, sizeof(message)/sizeof(uint8_t));

}

//--------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP-------MAIN LOOP--
void loop()
{

}//END LOOP-------------------
AngeloDM
  • 397
  • 1
  • 8