将uint8数组传递给函数会减小其大小(c ++代码)

时间:2014-04-12 19:42:11

标签: c++ arrays arduino arduino-ide

我在使用Arduino IDE编写的代码时遇到了问题。

当我尝试将长度为12的 uint8_t 类型的数组传递给函数时,我遇到了这个问题。传递数组时,它的大小似乎会减小(从12到4)。

我无法弄清楚这里发生了什么。任何帮助将不胜感激!

这是我的代码:

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-------------------

以下是串行监视器输出的内容:

传递给函数之前的原始消息是12个字节: 6,1,8,1,240,1,1,1,0,0,0,1,

传递给函数后,消息是4个字节: 6,1,8,1,

2 个答案:

答案 0 :(得分:1)

你不能在C中按值传递数组,因为几乎在每种情况下数组都会衰减到指向第i个元素的指针。
此外,除非您传递信息或它是合同的一部分,否则您无法知道任何函数参数指向的数组在调用者中的时间长度,无论如何这都是无用的信息。相反,传递一个参数,说明应该处理多少元素。

你得到sizeof MSG == 4,因为在你的平台上,这样的指针长4个字节。

答案 1 :(得分:0)

您必须将矢量大小传递给您的函数(您无法从指针MSG读取矢量大小,实际上在您的情况下sizeof(MSG) is equal to sizeof(uint8_t*)并且它取决于您的平台),因此您可以更改函数如下:

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-------------------