6

It may not be clear from the title. I came across the following code in an embedded STM32 project. I don't understand the line inside the function.

    static void txend1(UARTDriver *uartp) {
        (void)uartp; // what does this do? Is it a statement?
    }

I've tried searching elsewhere online, but most results are casting pointers to void pointers, which I don't think this is. Thanks for the help!

TowerFan
  • 210
  • 1
  • 10
  • 3
    It means "don't tell me I'm writing bad code, I *intended* not to use the argument". C compilers are a bit anal about it and tend to generate a warning. Often appropriately, but just not here. He did not want to do anything special at the end of the transmission, not unusual. The function pointer was not his choice. – Hans Passant Sep 20 '17 at 22:22
  • @HansPassant This may well be due to enforced API from external party and/or for uniformity sake - for example, there can be an alternative implementation that uses the pointer. – SomeWittyUsername Sep 20 '17 at 22:27
  • Yup, that's what "the function pointer was not his choice" means. – Hans Passant Sep 20 '17 at 22:29

1 Answers1

12

this is just a portable way to suppress the warning on this unused uart parameter.

It has no effect, but compilers see that as used, and don't issue any warning.

Very useful when the prototype of the function is imposed / cannot be changed (callback function) but your implementation doesn't need this parameter.

(note that gcc favors the __attribute__((unused)) construct, easier to understand, but not compatible with all compilers)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Hm...er..why not tell the compiler not to warn instead of coding nonsense? – savram Sep 20 '17 at 22:38
  • 2
    because you want warnings for places you werent aware of this is how you prevent it for the places you are. Some compilers have a pragma or directive, but gcc and others will let you just do x=x; or perhaps as in this case x; – old_timer Sep 20 '17 at 22:41
  • 2
    @savram You may want to be warn about other unused variables. – Gam Sep 20 '17 at 22:41
  • Still dirty to me. It's code that relies on specific compiler behavior. What if the compiler was smart enough to actually tell that the line is nonsense and the variable is still not being used? Portable, right...until it's not anymore. – savram Sep 20 '17 at 22:48
  • @Jean-François Fabre i was sure that you do not answer 10k times answered question. Some answers are many years old – 0___________ Sep 21 '17 at 01:25
  • @PeterJ_01 first I'm honoured that you thought that about me, unfortunately, I didn't know this duplicate (I'm not as good at it as with python), neither did the OP (difficult to find proper keywords) so that was a "weakness" moment. Note that this question is rare enough (compared to the `scanf` / `sizeof(ptr)` questions we see all the time). Let's hope the duplicate will be useful for future searches. – Jean-François Fabre Sep 21 '17 at 05:17
  • @savram I may be wrong, but the derefencing to `(void)` makes sure of that. `ptr,` alone _could_ be eliminated, but not that construct. And compilers are aware of this way to eliminate the warnings, they probably aren't going to break that. And if they do, you'll just get a warning, so no big risk. – Jean-François Fabre Sep 21 '17 at 05:19