101

In some C project, I have seen this code:

static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
    (void)ud;
    (void)osize;
    /* some code not using `ud` or `osize` */
    return ptr;
}

Do the two casts to void serve any purpose?

Antti Haapala
  • 117,318
  • 21
  • 243
  • 279
bastibe
  • 14,886
  • 24
  • 86
  • 118
  • Voting to close, as the correct answer (inhibiting compiler warnings about unused parameters) is in Charles's linked question. – T.E.D. Jan 10 '11 at 14:22
  • @Cody Gray - It was closed for that reason. However it was not in fact actually a dup of that question. 689677 was talking about casting returns to void, not parameters. – T.E.D. Jan 10 '11 at 14:25
  • 19
    Actually both duplicates are not valid to this question. One is C++, the other is regarding return values. **These are not the same things**. Are there any C parameter duplicates? – Matt Joiner Jan 10 '11 at 14:25
  • Agreed, upon further inspection. Which is exactly why tagging a question with both the C and C++ tags should be disallowed. – Cody Gray Jan 10 '11 at 14:26
  • @Cody Gray: That would be great :D – Matt Joiner Jan 10 '11 at 14:29
  • 2
    This is a different question than what the suggested duplicates covered. I can see why the mistake was made, though. Re-opened (obviously). – Tim Post Nov 15 '11 at 13:42
  • @PaulR: this is a C question. – Antti Haapala Aug 24 '17 at 07:22
  • @PaulR because the closure was incorrect. I had *just* a moment ago asked for the comments about wrong duplicate flag be **removed**. – Antti Haapala Aug 24 '17 at 07:26
  • See the [history](https://stackoverflow.com/posts/4647665/revisions). – Antti Haapala Aug 24 '17 at 07:29
  • 4
    **Notice: please do not close this as a duplicate of a C++ question as C++ uses `(void)` to somewhat different effect. This question is about C** – Antti Haapala Aug 24 '17 at 07:31

2 Answers2

92

It is there to avoid warnings from the compiler because some parameters are unused.

Benoit Thiery
  • 5,975
  • 3
  • 20
  • 27
  • 3
    What is the best way to suppress the warnings: http://stackoverflow.com/questions/3417837/what-is-the-best-way-to-supress-unused-variable-x-warning – Ciro Santilli新疆棉花TRUMP BAN BAD Sep 17 '14 at 07:12
  • @Benoit what does casting to void actually do? Is its only function to show the compiler that you're intentionally ignoring something or does (void) actually do something and when the compiler sees it, it'll just count it as having done something with the variable and therefore not issue a warning? – Tan Wang Sep 15 '16 at 14:20
  • 3
    @TanWang Its only function is to show the compiler that you're intentionally ignoring something. It will not do anything at runtime. – zwol Sep 27 '16 at 20:23
14

The reason for having unused parameters in the prototype is usually because the function needs to conform to some external API - perhaps it is a library function, or a pointer to that function is passed to another function that expects this calling convention. However not all arguments used by the calling convention are actually needed in the function itself.

The reason for mentioning the parameter name in the body is to avoid warnings like

unused.c: In function ‘l_alloc’:
unused.c:3:22: warning: unused parameter ‘ud’ [-Wunused-parameter]
 void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
                      ^~

This warning can be suppressed with using the actual parameter in the function body. For example if you do have the following statement:

ud;

This warning is now suppressed. However now GCC will produce another warning:

unused.c:5:5: warning: statement with no effect [-Wunused-value]
     ud;
     ^~

This warning tells that the statement ud;, while being syntactically valid C, does not affect anything at all, and is possibly a mistake, not unlike the statement

abort;

which should perhaps have been written as abort(); instead for it to do something.

And that's where the (void) cast comes in - it will tell the compiler unambiguously and explicitly that the statement is supposed to have absolutely no effect at all.

Antti Haapala
  • 117,318
  • 21
  • 243
  • 279