Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将uint8数组传递给函数可减小其大小(c+;+;代码)_C++_Arrays_Arduino_Arduino Ide - Fatal编程技术网

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

C++ 将uint8数组传递给函数可减小其大小(c+;+;代码),c++,arrays,arduino,arduino-ide,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};

我在使用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-------------------
int Serialbaud=19200;
int字节数;
uint8_t消息[]={0x06、0x01、0x08、0x01、0xF0、0x01、0x01、0x01、0x00、0x00、0x00、0x00、0x01};
//测试我们的信息传输(uint8_t阵列)
void testFunc(uint8\u t*MSG){
uint32_t len=sizeof(MSG)/sizeof(uint8_t);
Serial.println();
println(“传递到函数后,消息为”+字符串(len)+”字节:”;

对于(int i=0;i您不能在C中按值传递数组,因为几乎在所有情况下数组都会衰减为指向第一个元素的指针。
此外,除非您传递信息或它是契约的一部分,否则您无法知道任何函数参数指向的数组在调用方中的时间长度,这是无用的信息。相反,请传递一个参数,说明应该处理多少个元素


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

您必须将向量大小传递给您的函数(您不能从指针MSG读取向量大小,事实上在您的情况下
sizeof(MSG)等于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-------------------
int Serialbaud=19200;
int字节数;
uint8_t消息[]={0x06、0x01、0x08、0x01、0xF0、0x01、0x01、0x01、0x00、0x00、0x00、0x00、0x01};
//测试我们的信息传输(uint8_t阵列)
void testFunc(uint8_t*MSG,int len){
//uint32_t len=sizeof(MSG)/sizeof(uint8_t);
Serial.println();
println(“传递到函数后,消息为”+字符串(len)+”字节:”;

对于(int i=0;i@πάνταῥεῖ: 仍在添加更多信息,直到完成后才看到您的评论;-)已对您的第一个版本进行了升级,无需担心;-)…请查看答案并验证它们是否解决了您的问题。如果是,请检查答案。谢谢。