I recently found this line of code, but I don't know what the void with () mean. Can someone help me ? Thanks
(void) myFunc();
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.
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.
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.
Description. The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc.
(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.
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
return
s a value, but I don't care what it is."
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