We are working on project about Embedded Linux with C and C++ together. I recenty encountered a strange statement in a function:
bool StrangeFunction(void* arg1, void* arg2, void* arg3)
{
(void)arg1;
(void)arg2;
(void)arg3;
unsigned long keycode = (unsigned long)arg2;
switch(keycode)
{
...
I have two question in the code above
(void)arg1;
?unsigned long keycode = (unsigned long)arg2;
If you don't mind, i need some explanation and related links explaining the subjects. Thanks.
It is to silence compiler warnings about unused parameters.
It is possible but not portable. If on the given platform an address fits into unsigned long
, it's fine. Use uintptr_t
on platforms where it is available to make this code portable.
The cast to void
is mostly likely used to silence warnings form the compiler about unused variables, see Suppress unused-parameter compiler warnings in C/C++:
You should use uintptr_t
if possible instead of unsigned long, that is unsigned integer type capable of holding a pointer. We can see from the draft C99 standard section 7.18.1.4
Integer types capable of holding object pointers which says:
The following type designates an unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:
uintptr_t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With