if (radio.sendWithRetry(GATEWAYID, (const void*) (&Temps), sizeof(Temps)))
I don't understand "(const void*) (&Temps)? Can you explain that one to me? This is some way of pointing to the structure yes? Do you have any links that led you to this?
Truth be told:
- That part isn't my code, it was copied directly from Felix's Struct_send example code.
- I didn't really understand it either, but since you asked the question I looked it up.
From
http://www.learncpp.com/cpp-tutorial/613-void-pointers/The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:
The sendWithRetry function's job is simply to send to
another radio with a given ID number (arg1),
data from a section of memory (arg2),
some number of bytes (arg3). To avoid having to write a different send function for each type of data, Felix wrote it as taking a pointer to the data, and since it could be data of any type, it is a generic pointer.
From
http://stackoverflow.com/questions/5547131/c-question-const-void-vs-voidAs a general rule, if a function you write takes in a pointer to a value that you're not going to modify, then the function signature should use a const pointer. Using a pointer that isn't declared const means that the memory you're pointing to is allowed to be modified.
So, the way I understand it, if we were to send data to the function as a 'regular' variable, a copy would be sent to the function and we have no worry that it will modify the original data, But we would need to have different functions for different types, or cast the data from one type to another which is not really good programming practice. So we are instead sending a pointer to the location of the actual data. This means the function has the
opportunity to modify the data (whether it does or not), which could lead to unexpected results. To keep this from being an issue, the function call is written with the const to tell both the programmer and the compiler that the data will not be modified by the function.
Please, any of you who actually speak C, confirm or clarify.
LG