1

In our source code there are function calls like these where the return value gets cast to void:

(void) pthread_mutex_lock(&mutex);

What's the purpose of this?

GreatHam
  • 1,656
  • 3
  • 16
  • 21
  • 1
    Another duplicate that I found a bit better https://stackoverflow.com/questions/3035927/need-for-prefixing-a-function-with-void – Jens Gustedt Nov 30 '17 at 15:44

2 Answers2

3

This is not a functionality or anything. It is just an easy way to document or show the future coders or maintainers that the return value is ignored and the coder was aware of that. Nothing else except this.

Some function's (applying warn_unused_result attribute for gcc) enforce warning on ignoring implicitly the return value - this warning can be suppressed using void casting. (On some compiler versions (void)cast alone won't do - but that's different story)

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

Basically you don't have to do this.

Most of the time it is done because you know that a value is returned and to show that to other developers working on the project.

nollak
  • 131
  • 1
  • 2