This question already has an answer here:
这个问题在这里已有答案:
- Why does a C-Array have a wrong sizeof() value when it's passed to a function? [duplicate] 6 answers
- 为什么C-Array在传递给函数时有一个错误的sizeof()值? [重复] 6个答案
I'm having trouble with some code that I'm writing using the Arduino IDE.
我正在使用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).
当我尝试将长度为12的uint8_t类型的数组传递给函数时,我遇到了这个问题。通过阵列时,它的大小似乎减小了(从12到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,
传递给函数之前的原始消息是12个字节: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,
传递给函数后,消息是4个字节:6,1,8,1,
2 个解决方案
#1
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.
您无法在C中按值传递数组,因为几乎在每种情况下数组都会衰减为指向第一个元素的指针。此外,除非您传递信息或它是合同的一部分,否则您无法知道任何函数参数指向的数组在调用者中的时间长度,无论如何这都是无用的信息。相反,传递一个参数,说明应该处理多少元素。
You get sizeof MSG == 4
, because on your platform such pointers are 4 byte long.
你得到sizeof MSG == 4,因为在你的平台上这样的指针长4个字节。
#2
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:
你必须将矢量大小传递给你的函数(你不能从指针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-------------------
#1
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.
您无法在C中按值传递数组,因为几乎在每种情况下数组都会衰减为指向第一个元素的指针。此外,除非您传递信息或它是合同的一部分,否则您无法知道任何函数参数指向的数组在调用者中的时间长度,无论如何这都是无用的信息。相反,传递一个参数,说明应该处理多少元素。
You get sizeof MSG == 4
, because on your platform such pointers are 4 byte long.
你得到sizeof MSG == 4,因为在你的平台上这样的指针长4个字节。
#2
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:
你必须将矢量大小传递给你的函数(你不能从指针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-------------------