What does void *(*routine)(void *) mean in C?
argument-passing, c, function-pointers, pointers, syntax
Solution
void *(*routine)(void *);
declares a pointer to function that takes argument of type `void *` and returns pointer of type `void *`
Simple example:
#include <stdio.h>
void* foo(void* x) {
printf("Hello.");
}
int main(void) {
void *(*routine)(void *);
routine = foo; // assings foo to our function pointer
(*routine)(NULL); // invokes foo using this pointer
return 0;
}
outputs: `Hello.`
"If I wanted to pass this thing into a function" ~ here is example 2 for you:
#include <stdio.h>
void* foo(void* x) {
printf("Hello.");
}
typedef void *(*RoutinePtr)(void *); // alias to make your life easier
void routineInvoker(RoutinePtr routine) {
(*routine)(NULL); // invokes the routine
}
int main(void) {
RoutinePtr routine = foo; // creates a function pointer
routineInvoker(routine); // and passes it to our invoker
return 0;
}
Problem
I'm learning C and I came to this expression: ``` void *(*routine)(void *) ``` I find it very confusing. Maybe it's a pointer...to a pointer... to a pointer? If I wanted to pass this thing into a function, how would we manipulate it? I am trying to pass this routine construction as a parameter to a function that takes a `void(*)(void)`... but I am rather lost on what that actually means.