4

There are some c++ code

struct data {
    /* some fields */
};

typedef struct data * pData;

int function(pData pointer) {
    if(pointer == NULL)
        return ERROR;
    (void)pointer;
    /* other work */
}

What does "(void)pointer" mean?

Just for your information, there are some int/char*/etc, some functions pointers which are used as callback functions in the structure.

Near
  • 105
  • 6

4 Answers4

10

It is used to circumvent an unused-variable warning.

If you do use the variable, it is a no-op.

Mostly unused variables are parameters, that are required to fulfil a signature for a callback function, but not needed in your actual implementation.

Cf.

Update:

Just because it was not mentioned otherwise: The type of the variable might be anything. It is not constricted to pointer types.

Community
  • 1
  • 1
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • I believe that could simply omit the name of the parameter altogether if you do not use it. However, it can be interesting when there is a different set of used variables/parameters between Debug & Release configuration, for example. – Matthieu M. Mar 20 '13 at 16:06
  • 1
    @MatthieuM. I do not like omitting the name. For one it looks wrong (because it is wrong in C99). At one point you might use this parameters, and have to lookup the signature then. Other maintainers (or you, some weeks later) may look at the code and the perplexed what the meaning of some `int` or `bool` was. So, I like `(void)var` better. :-) – Kijewski Mar 20 '13 at 16:10
6

It doesn't mean a whole lot.

It evaluates the expression pointer, then explicitly ignores it by casting it to void.

Sometimes you see this construct when trying to convince a compiler not to warn for an un-used argument, but in your code the argument is already used since it's being NULL-checked.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

It casts the pointer value to a "no type" value, or that it is "absent of a type".

void foo(); // absent of a return value 
int bar(void); // absent of parameters
void *var; // Type of the variable pointed to is absent
(void) var; // The type of the variable is absent
0

It is a typical way to suppress unused variable compiler warning.

However, since the pointer is actually used as if(pointer == NULL), I see no reason do that.

If I have to guess, I would guess that the NULL check and return is added later than the warning suppression.

Arun
  • 19,750
  • 10
  • 51
  • 60