Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is (void) with () in C [duplicate]

Tags:

c

void

I recently found this line of code, but I don't know what the void with () mean. Can someone help me ? Thanks

(void) myFunc();
like image 820
PacCol Avatar asked Jun 23 '20 19:06

PacCol


People also ask

What is the use of void ()?

When used as a function return type, the void keyword specifies that the function doesn't return a value. When used for a function's parameter list, void specifies that the function takes no parameters.

What is void * in C?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is void * param?

It means the param of type void* (reference to a void), which is the size of a memory location . You can reference any memory location with this, which in practice anything.

Can you free a void * in C?

Description. The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc.


2 Answers

(void) has the form of a cast operation, but casting to void (note: not to void *) is normally not a useful thing to do.

In this context, though, (void) myFunc(); means that myFunc returns a value, and whoever wrote this line of code wanted to throw that value away and did not want the compiler to complain about this, and/or wanted to make it clear to future readers of the code that they were throwing the value away on purpose. In the generated code, (void) myFunc(); has exactly the same effect as myFunc(); with nothing in front.

Due to historic abuses of this notation, some compilers will warn you about not using the value of certain functions (e.g. malloc, read, write) even if you put (void) in front of them, so it's less useful than it used to be.

like image 68
zwol Avatar answered Oct 27 '22 07:10

zwol


myFunc probably returns something. Adding (void) to the the function call, (void)myFunc(), is a way to self-document the code. It means, "I know myFunc returns a value, but I don't care what it is."

like image 42
Fiddling Bits Avatar answered Oct 27 '22 06:10

Fiddling Bits