Strange arrow casting with void *

We are working together on an Embedded Linux project with C and C ++. I recently came across a strange expression 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 questions in the code above

  • What does it mean (void)arg1;?
  • Is it possible to use OK unsigned long keycode = (unsigned long)arg2;

If you don't mind, I need an explanation and links explaining the topics. Thank.

+4
source share
3 answers
  • It should disable compiler warnings about unused parameters.

  • It is possible, but not portable. If on this platform the address fits in unsigned long, this is normal. Use uintptr_ton platforms where it is available for portability of this code.

+10
source

void , . C/++:

uintptr_t, , unsigned long, , . C99 7.18.1.4 , , :

void , void, :

uintptr_t

+2

(void)arg1 , . , , , " ".

The second thing is used when passing an unsigned long to a function that accepts void *. If you're dealing with C ++ anyway, it's probably best to have an overloaded function. It also has the disadvantage of requiring the pointer to be at least equal unsigned long, and therefore it may not work on certain platforms.

0
source

Source: https://habr.com/ru/post/1545678/


All Articles