I'm confused about the meaning of void *function()
.
Is it a pointer to function or a function returning void*
? I've always used it on data structures as a recursive function returning a pointer, but when i saw a code in multithreading (pthread
) there is a same function declaration. Now I'm confused what's the difference between them.
Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.
A void function can return A void function cannot return any values. But we can use the return statement. It indicates that the function is terminated.
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 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.
Functions that do not return a value. Most commonly, void is used to indicate that a function does not return a value: void writeValue(int x) // void here means no return value { std::cout << "The value of x is: " << x << '\n'; // no return statement, because this function doesn't return a value }
Void Functions in C. Functions may be return type functions and non-return type functions. The non-return type functions do not return any value to the calling function; the type of such functions is void. These functions may or may not have any argument to act upon. A few illustrations of such functions are given below.
A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. // typecasted to any type like int *, char *, ..
You may or may not use the return statement, as there is no return value. Even without the return statement, control will return to the caller automatically at the end of the function. A good utilization of a void function would be to print a header/footer to a screen or file.
Return Function Pointer From Function: To return a function pointer from a function, the return type of function should be a pointer to another function. But the compiler doesn’t accept such a return type for a function, so we need to define a type that represents that particular function pointer.
The function has the return type void *
.
void *function();
So I always prefer in such cases to separate the symbol *
from the function name like
void * function();
And as Jarod42
pointed to in a comment you can rewrite the function declaration in C++ using the trailing return type like
auto function() -> void *;
If you want to declare a pointer to function then you should write
void ( *function )();
where the return type is void
Or
void * ( *function )();
where the return type void *
.
Or a pointer to function that returns pointer to function
void * ( *( *function )() )();
Whenever I'm unsure about C syntax issues, I like to use the cdecl utility (online version) to interpret for me. It translates between C syntax and English.
For example, I input your example of void *foo()
and it returned
declare foo as function returning pointer to void
To see what the other syntax would look like, I input declare foo as pointer to function returning void
and it returned
void (*foo)()
This gets particularly useful when you have multiple levels of typecasts, stars, or brackets in a single expression.
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