in the following function
int f (some_struct* p)
{
(void) p;
/* something else */
return 0;
}
what does the statement
(void) p;
mean?
Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.
In C, When you convert the data type of a variable to another data type then this technique is known as typecasting. Let's say that you want to store a value of int data type into a variable of float data type. Then you can easily do this with the help of typecasting.
Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable.
Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.
The statement does nothing at runtime, and results in no machine code.
It suppresses a compiler warning that p
is unused in the body of the function. This is a portable and safe way to suppress this warning across a variety of different compilers, including GCC, Clang, and Visual C++.
“Cast to void
” is a C language idiom that, by convention, suppresses compiler and lint
warnings about unused variables or return values.
In this case, as Dietrich Epp correctly points out, it tells the compiler that you know that you're not using the argument p
, and not to give you “unused argument” warnings about it.
The other use of this idiom, casting the return value of a function to void
, is the traditional way of telling lint
or, more importantly, other programmers that you'd made a conscious decision not to bother checking the return value of a function. For example:
(void)printf("foo")
Would mean “I know printf()
returns a value, and I should really check it, but I've decided not to bother”.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With