tags:

views:

246

answers:

4
static void llist_dtor(void *user, void *element)
{
  (void)user;
  (void)element;
  /* Do nothing */
}

Is it no-operation function? Then why is casting done? Is it ok to pass NULL as one of its parameters?

+5  A: 

Yes, this is a no-op function.

The casting is a common trick to prevent the compiler complaining about unused parameters.

Oli Charlesworth
+14  A: 

That's indeed a no-op. The casts to (void) are here to avoid getting "parameter never used" warnings with some compilers (the casts are optimized away, but the parameters are still considered as "used").

You can pass NULL since the parameters are ignored anyway.

Frédéric Hamidi
A: 

That is not no-op. Like that you tell the compiler to ignore those two arguments.

VJo
+3  A: 

Yes, this is a no-op function and void casted lines are placed to avoid the "unused parameter" warning. For gcc, search for "unused" in the page: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

However, if it were C++ instead of C, I would probably write it little differently as

static void llist_dtor( void * /* user */, void * /* element */ )
{
  /* Do nothing */
}

Note that the variable names are commented out.

ArunSaha
-1, gcc won't compile C code without argument names. (Works for C++ though).
Andrew Medico
@Andrew Medico: +1: Thanks for correcting me. Mostly I do C++ and there I have done this few times... Never thought this could be different in C.
ArunSaha