Hello All,
when I write "uint8_t *" as in for example: Wire.send((uint8_t *)&var1, sizeof(var1)); what does the * after uint8_t mean?
Many thanks,
Rui
Hello All,
when I write "uint8_t *" as in for example: Wire.send((uint8_t *)&var1, sizeof(var1)); what does the * after uint8_t mean?
Many thanks,
Rui
It means that you are providing a pointer to var1 (or "the address of var1") to the function, instead of the actual value of var1. This permits Wire.send() to work on data of any length. It's not very relevant when only sending 8 bits, but it provides a lot of flexibility.
Specifically, the passed var as described: (uint8_t *)&var1
means:
var1 is the variable you are sending to the function. It could almost be anything considering the context of the pass. It could be an 8 bit unsigned int, it could be a float, hell... it could be a char array.
The use of (), a variable type, and the : (uint_t) is a cast. It says "Treat whatever I type next as if it was a pointer to an 8 bit unsigned int."
Casting is a mechanic where by you can say "Ok, I KNOW that this isn't the variable type the function wants, but trust me... it will fit."
The actual function variable, &var1, says "Address of the variable named var1."
So, in summation, (uint8_t *)&var1 says "Take the address of the variable var1, act as if it's a pointer to an unsigned 8 bit integer, and pass this pointer to the function."
This is a fairly complicated example of why the C language is SO powerful. Know pointer manipulation and indirection, and you know C.
That is a type cast. It casts type uint8_t * to the address of var1. What the type cast does is of no interest to you. You can't change how to write it anyway. But the &var1 should interest you. Like westfw said, &var1 is the memory location (address) of the var1. Having this memory location allows the recipient to modify var1's value, not just getting a copy if var1's value, which can be modified but the modification is on the copy not the original.