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

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

问题描述

这是输入过程和静态类型的扩展uint8_t 数组关于从建议的解决方案中遇到的问题.

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

我正在使用:

#include 

Int 变量代码(有错误):

int world = 1;静态 uint8_t hello[sizeof(world)];memcpy(你好,&world,sizeof(你好));

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

 memcpy(hello, &world, sizeof(hello));^退出状态 1'(' 标记之前的预期构造函数、析构函数或类型转换

在阅读了一些关于这个问题的文章后,我发现这必须放在 loop() 中,所以我这样做了.结果是编译和上传时没有问题,但是当我添加该行时它打印了值 1073488876:

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

我也做过:

Serial.println(sizeof(hello));

它打印了 4,这意味着代码将变量hello"检测为一个 int(因为一个 int 是 4 个字节).

有趣的是,如果我注释掉 memcpy 行,我会得到相同的结果,即 1073488876,因此代码在放置在 loop() 函数中时以某种方式忽略"了该行.>

字符串变量代码

String world = "Hello";//6 个字符,包括 \0静态 uint8_t 你好[6];world.toCharArray((char *)hello, sizeof(hello));

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

 world.toCharArray((char *)hello, sizeof(hello));^退出状态 1'world' 没有命名类型

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

串行打印也适用于该行:

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

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

解决方案

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

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

<块引用>

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

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

如果您希望 println 打印 1,您可以将字节数组转换回 int,就像将其转换为数组一样:

int world = 1;静态 uint8_t hello[sizeof(world)];memcpy(你好,&world,sizeof(你好));//转换回来国际世界2;memcpy(&world2, 你好, sizeof(world2));//打印Serial.println(world2);

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.

解决方案

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

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

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