Int variable input error for static uint8_t array

我是研究僧i 提交于 2019-12-11 15:49:11

问题


This is an extension from Input process and type for static uint8_t array regarding issues experienced from the suggested solution.

Currently, I am trying to create both a int variable and a string variable with is inputted into a static uint8_t array and then is printed using Serial.println.

I am using:

#include <U8x8lib.h>

Int Variable Code (Has Error):

int world = 1;
static uint8_t hello[sizeof(world)];
memcpy(hello, &world, sizeof(hello));

If I copy this directly and paste it into the IDE outside of both loop() or setup(), I get the following error:

    memcpy(hello, &world, sizeof(hello));
       ^
exit status 1
expected constructor, destructor, or type conversion before '(' token

After doing some reading about this issue, I discovered that this must be put in loop(), so I did. The result was that no issues were present when compiling and uploading, however it was printing the value 1073488876 when I added the line:

Serial.println(int((uint8_t*)hello));

I also did:

Serial.println(sizeof(hello));

And it printed 4, which means the code is detecting the variable "hello" as being an int (as an int is 4 bytes).

Interestingly enough, if I comment out the memcpy line I get the same result, being 1073488876, so the code is somehow "ignoring" the line when placed in the loop() function.

String Variable Code

String world = "Hello"; // 6 chars including \0
static uint8_t hello[6];
world.toCharArray((char *)hello, sizeof(hello));

If I copy this directly and paste it into the IDE outside of both loop() or setup(), I get the following error:

 world.toCharArray((char *)hello, sizeof(hello));
 ^
exit status 1
'world' does not name a type

If I put the line world.toCharArray((char *)hello, sizeof(hello)); in loop(), it works.

Serial print also works with the line:

  Serial.println(String((char *)hello));

I don't know if this has any relationship to my issue with the Int Variable code, but thought I might as well show it.


回答1:


If I copy this directly and paste it into the IDE outside of both loop() or setup(), I get the following error:

The space outside functions could be used for declaration of functions, variables and classes but not for the execution of the code, it has to go inside functions. loop() and setup() are functions called from main function, this is why it works and doesn't work outside of them.

it was printing the value 1073488876 when I added the line:

hello was declared as an array hello[sizeof(int)]. Arrays have tendency to decay to the pointer to its first element. When you pass (uint8_t *)hello to the functional cast expression int(...) and converting the array hello to the pointer to its first element explicitly with (uint8_t *) casting, it could have been done for you automatically. int(hello) or int((uint8_t *)hello) are basically the same. int(...) is a cast, as mentioned above, that turns your pointer uint8_t * into int so the value you see is memory address of the first element presented as int.

If you want println to print 1 you could convert the array of bytes back into int the same way you converted it to array:

int world = 1;
static uint8_t hello[sizeof(world)];
memcpy(hello, &world, sizeof(hello));

//convert back
int world2;
memcpy(&world2, hello, sizeof(world2));

//print
Serial.println(world2);


来源:https://stackoverflow.com/questions/52328494/int-variable-input-error-for-static-uint8-t-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!