What is cast cast operator in C?

in the next function

int f (some_struct* p) { (void) p; /* something else */ return 0; } 

what makes the statement

 (void) p; 

mean?

+8
c
source share
4 answers

The operation does nothing at runtime and does not produce machine code.

It suppresses the compiler warning that p not used in the function body. This is a portable and safe way to suppress this warning in many different compilers, including GCC, Clang, and Visual C ++.

+12
source share

"Cast to void " is a C idiom that, by convention, suppresses the compiler and lint warnings about unused variables or return values.

In this case, as Dietrich Epp correctly points out, he tells the compiler that you know that you are not using the p argument, but are not warning you about “unused arguments”.

Another use of this idiom that converts the return value of a function to void is the traditional way of telling lint or, more importantly, other programmers that you have made the conscious decision not to worry about checking the return value of the function. For example:

 (void)printf("foo") 

It will mean that "I know that printf() returns a value, and I really have to check it, but I decided not to bother."

+2
source share

He used to avoid a warning about an unused function parameter. It is simply discarded, does nothing, except that the expression has a side effect.

C11 §6.3.2.2 void

the (non-existent) value of the void expression (an expression that is of the void type) should not be used in any way, and implicit or explicit conversions (with the exception of void) should not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designation is discarded. (A void expression is evaluated for its side effects.)

Another way to avoid a warning about an unused function parameter:

 p = p; 
0
source share

void indicates that the compiler or lint does not give a warning. If the variable will never be used, the compiler or lint will prompt you to delete it.

If you do not want to delete it, you can use void. As a link: How can I hide “specific but not used” warnings in GCC?

0
source share

All Articles