ghayalcoder
ghayalcoder

Reputation: 1735

What does the following code do?

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?

Upvotes: 6

Views: 918

Answers (4)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262919

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.

Upvotes: 15

Arun
Arun

Reputation: 20383

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.

Upvotes: 3

BЈовић
BЈовић

Reputation: 64203

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

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272457

Yes, this is a no-op function.

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

Upvotes: 4

Related Questions