Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a casted parameter statement in C?

Tags:

c

in the following function

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

what does the statement

(void) p; 

mean?

like image 442
user693986 Avatar asked Aug 30 '13 00:08

user693986


People also ask

What does cast mean in C?

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.

Can you cast variables in C?

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.

What does it mean to cast a variable?

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.

What does casting int () do in C?

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.


2 Answers

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++.

like image 167
Dietrich Epp Avatar answered Nov 05 '22 17:11

Dietrich Epp


“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”.

like image 20
Emmet Avatar answered Nov 05 '22 17:11

Emmet