Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting function returns to void

Tags:

Many times I see in open source code that a call to a C function is cast to void.

For example, in the source code for ls (http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/ls/ls.c) I see the following.

(void) setlocale(LC_ALL, "");

Why is this good practice?

like image 957
Russell Avatar asked Oct 22 '10 16:10

Russell


People also ask

What does casting to void mean?

Casting to void is used to suppress compiler warnings. The Standard says in §5.2. 9/4 says, Any expression can be explicitly converted to type “cv void.” The expression value is discarded. Follow this answer to receive notifications.

Why do we use void as it is returning nothing?

Return from void functions in C++ The void functions are called void because they do not return anything. “A void function cannot return anything” this statement is not always true. From a void function, we cannot return any values, but we can return something other than values. Some of them are like below.

What is the point of void C++?

When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal." If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword.


1 Answers

It explicitly means you ignore the return value, and did not just forget it.

like image 65
zneak Avatar answered Oct 06 '22 04:10

zneak