Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "(void)pointer;" mean in c++?

Tags:

c++

pointers

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.

like image 412
Near Avatar asked Dec 05 '22 11:12

Near


2 Answers

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.

  • unused parameter warnings in C code
  • GCC manual: -Wunused-variable (enabled by -Wall, too).

Update:

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

like image 91
kay Avatar answered Dec 25 '22 20:12

kay


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.

like image 32
unwind Avatar answered Dec 25 '22 19:12

unwind