Int variable input error for static uint8_t array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Int variable input error for static uint8_t array



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.


int


string


static uint8_t array


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:


loop()


setup()


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:


loop()


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.


loop()



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:


loop()


setup()


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. 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.


world.toCharArray((char *)hello, sizeof(hello));


loop()




1 Answer
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.


loop()


setup()



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.


hello


hello[sizeof(int)]


(uint8_t *)hello


int(...)


hello


(uint8_t *)


int(hello)


int((uint8_t *)hello)


int(...)


uint8_t *


int


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:


println


int


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





It works @Killzone. I just had a question regarding the memory location process. The & symbol defines the memory location of a variable. I see that you use &world2 when converting back, but not for hello when initially making the variable hello the value of world. To me it looks inverse. Why aren't all the variables in the memcpy lines given a & at the start or why isn't it consistent? I assume it has something to do with the conversion back to an int. I have tested the idea of putting an & at the front of hello, world and world2 and it does not make a difference.
– Lachlan Etherton
2 hours ago



&


&world2


hello


world


memcpy


&


&


hello


world


world2






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS