静态uint8_t数组的int变量输入错误 [英] Int variable input error for static uint8_t array

查看:666
本文介绍了静态uint8_t数组的int变量输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是>静态输入过程和类型的扩展uint8_t阵列,涉及建议解决方案中遇到的问题.

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

当前,我正在尝试创建一个int变量和一个string变量,将其输入到static uint8_t array中,然后使用Serial.println打印.

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.

我正在使用:

#include <U8x8lib.h>

整数变量代码(有错误):

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

如果直接将其复制并粘贴到loop()setup()之外的IDE中,则会出现以下错误:

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

在阅读了有关此问题的内容后,我发现必须将其放在loop()中,所以我这样做了.结果是在编译和上传时没有问题,但是当我添加以下行时,它的打印值是1073488876:

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

我也做了:

Serial.println(sizeof(hello));

它打印了4,这表示代码正在将变量"hello"检测为int(因为int为4个字节).

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

足够有趣的是,如果我注释掉memcpy行,我得到的结果是1073488876,所以当放在loop()函数中时,代码会以某种方式忽略"该行.

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 world = "Hello"; // 6 chars including \0
static uint8_t hello[6];
world.toCharArray((char *)hello, sizeof(hello));

如果直接将其复制并粘贴到loop()setup()之外的IDE中,则会出现以下错误:

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

如果将world.toCharArray((char *)hello, sizeof(hello));行放在loop()中,它将起作用.

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

我不知道这与我的Int Variable代码问题是否有任何关系,但我想也应该展示一下.

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.

推荐答案

如果我直接将其复制并粘贴到两者之外的IDE中 loop()或setup(),出现以下错误:

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

函数外部的空间可以用于函数,变量和类的声明,但不能用于代码的执行,它必须位于函数内部. loop()setup()是从主函数调用的函数,这就是为什么它可以工作,而不能在它们之外工作的原因.

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.

当我添加以下行时,它正在打印值1073488876:

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

hello被声明为数组hello[sizeof(int)].数组倾向于衰减指向其第一个元素的指针.当您将(uint8_t *)hello传递给功能转换表达式 int(...)并进行转换时数组hello(uint8_t *)强制转换显式指向其第一个元素的指针,它可能已经为您自动完成. int(hello)int((uint8_t *)hello)基本相同.如上所述,int(...)是强制类型转换,它将指针uint8_t *转换为int,因此您看到的值是表示为int的第一个元素的内存地址.

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.

如果要println打印1,则可以将字节数组转换回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);

这篇关于静态uint8_t数组的int变量输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆