【问题标题】:Passing array of uint8 to function reduces its size (c++ code) [duplicate]将 uint8 数组传递给函数会减小其大小(c++ 代码)[重复]
【发布时间】:2014-04-12 19:42:11
【问题描述】:

我在使用 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,

【问题讨论】:

  • 请查看答案并验证它们是否能解决您的问题。如果是,请检查已回答。谢谢。

标签: c++ arrays arduino arduino-ide


【解决方案1】:

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

您会得到sizeof MSG == 4,因为在您的平台上,此类指针的长度为 4 字节。

【讨论】:

  • @πάνταῥεῖ:仍在附加更多信息,直到完成后才看到您的评论;-)
  • 已经支持你的第一个版本,不用担心 ;-) ...
【解决方案2】:

您必须将向量大小传递给您的函数(您不能从指针 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-------------------

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 2011-03-04
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    相关资源
    最近更新 更多