Pointer to Function Return - c

Assume the following function prototype:
void *function(int arg).
Is that a pointer to a function? If so, how can this return a value, if it's return type is void? The description of the function says that it may return a positive integer or NULL. Since these are two different "types", would that be the reason it's void? To avoid typecasting?

No, that's not a pointer to a function. It's a function that returns void *, also known as a generic pointer.
A pointer to a function that returns void would be this:
void (*function)(int arg);
And that function wouldn't return anything.

It is not a pointer to a function. It says that it returns a void pointer. The advantage of a function returning a void * is that you can cast it to any type in the function. See here for an explanation.

It is not a pointer to a function; it's a function that returns a pointer to void: i.e., an undifferentiated pointer to raw memory. A pointer to a function returning void would be void (*function)(int arg) -- those parentheses change the meaning entirely.

void *function(int arg);
is not a pointer to a function. It is the prototype of a function that returns a void * and has an int parameter.

No, it's a prototype specifying that the function function takes a single int argument and returns a void * value.
A pointer to a function would look like this:
void *(*fn)(int arg);
This creates a pointer fn which points to a function matching the properties of function.

Related

What does this expression: void* (*fct)(void*(*)(void*), void*)?

I know that e.g.
void *(*myFuncName)(void*)
is a function pointer that takes and also returns void*.
Is this a pointer which takes 2 arguments?
A void pointer another function of that type returning void* and a void*?
I'm just guessing..
void* (*fct)(void*(*)(void*), void*);
declare fct as a pointer to a function that
returns a void *
expects its first argument is of type pointer to a function that expects a void * and returns a void * and
expects its second argument is of type void *.
void* (*fct)(void*(*)(void*), void*)
// 44444 2111 333333333333333333333333
fct (1) is a pointer (2) to a function (3*) that returns a pointer (4).
(*) The function parameters are void*(*)(void*) and void*
void*(*)(void*) a pointer to a function that takes a pointer argument and returns a pointer
void* a pointer
cdecl.org and my compiler both agree. It's a syntax error. There are more close parentheses than open ones.
A function pointer named x that returns void * and takes two parameters of type void * would look like:
void *(*x)(void *, void *);
Read to the left:
myFuncName is a pointer. What is it a pointer to?
Read to the right:
myFuncName is a pointer to a function which takes two arguments, one of which is a function pointer (with one void* argument and returning void*, similar to myFuncName), and the other one being of type *void*, returning void*. What does the function return?
Read to the left:
myFuncName is a pointer to a function which takes two arguments (see above) and returns void*
As https://stackoverflow.com/users/47453/bill-lynch pointed out, cdecl will tell you the same, as long as your syntax is correct.
Best regards
Andreas

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.

function declared as void* foo()

I've come across this piece of code in my O.S book:
void *foo()
{
// does something
pthread_exit(NULL);
}
What is the meaning of void *foo?
Does that mean the function return a pointer to something of type void?
No, it means that the function returns an pointer of the untyped type - void*.
This is a "generic" pointer type. Any pointer to data can be cast to void*, and returned back to the caller. However, in order to dereference the pointer, you must cast it to a non-void pointer type (int*, long*, char*, and so on).
void * means it returns a pointer to some type, it isn't specified which. In order to be used the pointer is casted to the appropriate type and then used. The fact that void *foo is just a matter of spacing.
Check out this explanation

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.

C - Strange Prototype Argument

What's going on in this function prototype? Obviously the void parameter with some sort of typecasting is confusing...
int *my_func(my_struct *m, void (*m_op)(my_struct *v, void arg));
The second argument to the function my_func is a pointer to a function that returns no value (void), but which takes two arguments, a my_struct pointer and ... and (an invalid) void. The latter should probably be void *arg; you cannot have a variable or argument of type void. As it stands, the code should not compile.
This prototype declares a function, my_func that returns int *. It takes two arguments, the first being of type my_struct * and the second of the strange type void (*)(my_struct *, void). This means that the second argument is a pointer to a function that returns void and takes 2 arguments itself, a pointer to my_struct and void (I assume that was a typo and it takes a void *).
This little article explains how to parse C declarations in a spiral-like motion. Constructing is done in the reverse.
My suggestion - always try to split declarations into smaller ones - in that case code will be more readable. In this case you could re-write code as:
typedef struct {} my_struct;
typedef void (* m_op_function)(my_struct * v, void * arg);
int * my_func(my_struct * m, m_op_function f);
And as everybody said- it's almost 99,99% typo here regarding second parameter to m_op_function- it is possible void* - so that you can pass any pointer to it - be it (char*), (int*), (my_struct*), or anything else. Simply just cast pointer.

Resources