113

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?

4
  • 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? Commented Jan 10, 2011 at 14:25
  • Agreed, upon further inspection. Which is exactly why tagging a question with both the C and C++ tags should be disallowed. Commented Jan 10, 2011 at 14:26
  • See the history. Commented Aug 24, 2017 at 7: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 Commented Aug 24, 2017 at 7:31

2 Answers 2

108

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

Sign up to request clarification or add additional context in comments.

4 Comments

What is the best way to suppress the warnings: stackoverflow.com/questions/3417837/…
@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?
@TanWang Its only function is to show the compiler that you're intentionally ignoring something. It will not do anything at runtime.
Anyone knows where this is documented? Language specyfication? Compiler manual? Does it have a name (such statement)?
20

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.