Function pointer with (void*) - c

In C what's the function pointer (void*) doing in:
int (*fn) (void*)
If the parameter is nothing then it should be:
int (*fn) ()
My understanding is void* is chunk of memory. void* mem means mem pointing to a chunk of memory. But what's (void*) without a name?

That function pointer declaration does not require you to give the void* a name. It only requires a type to define the argument list.
This is similar to how:
void my_function(int x);
is just as valid as
void my_function(int);

void * is an anonymous pointer. It specifies a pointer parameter without indicating the specific data type being pointed to.

An unnamed parameter is not the same thing as no parameters. The declaration:
int (*fn)(void*);
merely states that fn is a function pointer that takes a void* argument. The parameter name is inconsequential (and is only meaningful in the function implementation where it's the name of the local variable).
(Although it the parameter name is not necessary in function declarations, it is nevertheless useful to people reading the code to identify what the parameter is.)

It means that fn is "any function that takes a block of memory as a parameter, and returns an int"

Related

Casting functions

I have the following functions:
typedef int (*Bar)(void *, int a);
int foo (Struct *s, int b);
void *call(Bar c);
What does it mean when I call the function:
call( (Bar) foo);
typedef int (*Bar)(void *, int a);
This defines Bar an alias for a pointer-to-function type, specifically for a function that takes two arguments of type void* and int and returns a result of type int.
int foo (Struct *s, int b);
This declares (but does not define) a function named foo that takes two arguments as specified.
I presume Struct is a typedef, but it's a poor name, easily confused with the struct keyword.
void *call(Bar c);
This declares a function call that takes an argument of type Bar and returns a void* result.
call( (Bar) foo);
foo is the name of the function declared earlier. In this context, it's treated as an expression of pointer-to-function type. This pointer value is converted to type Bar. Since `Bar is a pointer-to-function type, the conversion is well defined.
That converted pointer is then passed to a function named call. (call returns a void* result, but it's quietly ignored here; is that what you intended?)
We don't know what the call function does with its argument, so we can't tell what the behavior of this statement might be.
A conversion from one function pointer type to another, using a cast, is well defined, and converting back to the original type will yield the original value. Calling a function using a function pointer with the wrong type has undefined behavior. (You might be able to get away with it in some cases, but don't.)
Your call function could try to make a call via its parameter, but that call would have undefined behavior unless that parameter is converted back to the correct type.
There isn't enough information in your question to know what's going on, and the confusing names you've chosen don't help.

C programming: void * argument is it really pointer to anything, can I pass function pointer there

I consider how should I use void * correctly (I have some break from C language). As I remember void * is used to enable passing any argument to callback function. I know that it is ok to pass something like primitive types int *, const char * or even collection of values as struct attributes *.
I consider whether passing pointer to function as void * will be ok.
ex.
typedef void (*callback_t)(void *);
typedef void (*my_custom_callback_t)( /* list of params */ )
void someAPIFunction( /*... */, callback_t callback, void *callback_param);
void standard_callback(void *param) {
((my_custom_callback_t) param) ( /* my params */ );
}
I consider whether having such types it will be correct to pass my_custom_callback as param to standard_callback?
void my_custom_callback(/* list of params */) { }
someAPIFunction( /*...*/, standard_callback, my_custom_callback);
No, you cannot pass function pointer as void *.
6.3.2.3 Pointers
A pointer to void may be converted to or from a pointer to any object type. A pointer to
any object type may be converted to a pointer to void and back again; the result shall
compare equal to the original pointer.
and
A pointer to a function of one type may be converted to a pointer to a function of another
type and back again; the result shall compare equal to the original pointer. If a converted
pointer is used to call a function whose type is not compatible with the referenced type,
the behavior is undefined.
Notice how there is no mention of possibility to mix object and function pointers. It may seem to work on some system, but it's undefined behaviour and not guaranteed to work.
However, you can create wrapper object and pass address of that:
struct wrapper {
my_custom_callback_t func;
};
struct wrapper my_wrapper = { my_custom_callback };
someAPIFunction( /*...*/, standard_callback, &my_wrapper);
Or just pass pointer to function pointer (function pointer variable is object type):
my_custom_callback_t func = my_custom_callback;
someAPIFunction( /*...*/, standard_callback, &func);
Most platforms will in fact allow you to pass a function pointer as a void *, but it's not theoretically allowed. Code might have different pointers to data. You also cannot pass a const-qualified address to a void *.
As pointed out already, this is not allowed in the general case and you will probably get a compiler warning. This is because there are platforms where function pointers are larger than data pointers.
The much more common case however is that the pointer sizes are indeed equal. Interfaces for accessing shared objects like dlsym() or GetProcAddress() actually rely on that.
So if you are sure that all your target platforms will use the same size for function pointers as for data pointers, you can make it explicit (so the compiler doesn't issue any warnings) by first casting to e.g. uintptr_t. The cast to and from an integer is allowed for all pointer types, and as long as the integer is large enough to hold the pointer, well-defined. uintptr_t is guaranteed to be large enough for a data pointer, so if the function pointer is the same size, it's fine.

What does method(void * param) mean in C?

I need to know what it means when a function has a void * parameter. For example:
function(void * param){}
It is a function that receives a void*. This may be used to pass any kind of pointer in C.
A good example is the pthread_create function, and the pthread start routine.
Note, however, that if no return type is specified, it defaults to return an int, which it does not in your example.
param is a void pointer means pointer to any data type . You may call generic type pointer.
e.g.
func(void *param)
{
// body
}
call like this :
char* cptr;
int* iptr;
float* fptr;
func(cptr);
func(iptr);
func(fptr);
A void * is a pointer to any data, i.e. to data of an unknown or unspecified type.
param has type void *, which in C serves as a "generic" pointer type; any object pointer type can be converted to void * and vice versa without need for an explicit cast (some implementations allow function pointer types to be converted to void *, but that's not universal and not guaranteed by the language standard).
Note that a void pointer may not be dereferenced, so you can't do something like x = *param in the body of the function. You'd have to convert the pointer to a different type such as int * or double * or whatever to get what it points to. And that depends on what function is supposed to do with it.
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.

What does it mean when two voids are in a parameter name in C?

I have a method I need to fill in and one of the parameters is: "void (*destructor)(void*)". Can anyone tell me what this is?
It is a Function pointer.
It stores address of an function which takes a void *(pointer to void) as an input parameter and returns no parameter.
That's a function pointer. The function given should have the signature:
void funcname(void*)
The first void is the return type, the second is a pointer to void (in other words, a pointer to who knows what, which the underlying function will presumably cast to some useful type later).
The void (*destructor)(void*) is a function pointer. This means, that you can use it to point to any function, which has void as return value (returns nothing) and expects an generic pointer (void*), which can point to anything.
It's pointer to a function that takes a void* as its only argument, and returns nothing.

What is the function of an asterisk before a function name?

I've been confused with what I see on most C programs that has unfamiliar function declaration for me.
void *func_name(void *param){
...
}
What does * imply for the function? My understanding about (*) in a variable type is that it creates a pointer to another variable thus it can be able to track what address at which the latter variable is stored in the memory. But in this case of a function, I don't know what this * asterisk implies.
The asterisk belongs to the return type, and not to the function name, i.e.:
void* func_name(void *param) { . . . . . }
It means that the function returns a void pointer.
The * refers to the return type of the function, which is void *.
When you declare a pointer variable, it is the same thing to put the * close to the variable name or the variable type:
int *a;
int* a;
I personally consider the first choice more clear because if you want to define multiple pointers using the , separator, you will have to repeat the * each time:
int *a, *b;
Using the "close to type syntax" can be misleading in this case, because if you write:
int* a, b;
You are declaring a pointer to int (a) and an int (b).
So, you'll find that syntax in function return types too!
The * belongs to the return type. This function returns void *, a pointer to some memory location of unspecified type.
A pointer is a variable type by itself that has the address of some memory location as its value. The different pointer types in C represent the different types that you expect to reside at the memory location the pointer variable refers to. So a int * is expected to refer to a location that can be interpreted as a int. But a void * is a pointer type that refers to a memory location of unspecified type. You will have to cast such a void pointer to be able to access the data at the memory location it refers to.
It means that the function returns a void*.

Resources