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?
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.
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.
main(void) will be called without any parameters. If we try to pass it then this ends up leading to a compiler error.
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.
It is there to avoid warnings from the compiler because some parameters are unused.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With