Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cast an unused function parameter value to void?

Tags:

c

casting

void

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?

like image 458
bastibe Avatar asked Jan 10 '11 14:01

bastibe


People also ask

Why do we use void in the argument list of a function?

If a function is not meant to take any parameters, specify that by using void in the parameter list. int printf(const char*, ...); declares a function that can be called with varying numbers and types of arguments.

What does casting to void do in C?

Casting to void is used to suppress compiler warnings. The Standard says in §5.2. 9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded. Follow this answer to receive notifications.

Is void parameter necessary?

main(void) will be called without any parameters. If we try to pass it then this ends up leading to a compiler error.

What is unused parameter?

Reports the parameters that are considered unused in the following cases: The parameter is passed by value, and the value is not used anywhere or is overwritten immediately. The parameter is passed by reference, and the reference is not used anywhere or is overwritten immediately.


1 Answers

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

like image 104
Benoit Thiery Avatar answered Nov 09 '22 14:11

Benoit Thiery