Why cast an unused function parameter value to void?

bastibe picture bastibe · Jan 10, 2011 · Viewed 25.3k times · Source

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?

Answer

Benoit Thiery picture Benoit Thiery · Jan 10, 2011

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