function declared as void* foo() - c

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

Related

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 return type should void *function()s use?

Both of these tests don't give me errors when I compile them yet they are returning different types of pointers.
void *testone(void *x)
{
return (x);
}
void *testtwo(void *x)
{
char *y;
return (y);
}
This is particularly confusing because I am trying to recreate the memcpy function and I can't tell if I should return a void pointer, or an unsigned char pointer.
In C, any object pointer type can be converted to void *, and void * can be converted to any object pointer type without any cast.
Your code obeys those rules; it is OK. The compiler should not complain.
ISO/IEC 9899:2011 §6.3.2.3 Pointers:
¶1 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.
Note that 'object' excludes functions — function pointers have separate rules:
¶8 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.

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

Why does a void pointer point to anything?

When something is cast to void, then the value becomes NULL. However, why does a void * point to any data type? Shouldn't a void pointer just be useless?
A void pointer is a pointer to anything. It is a generic pointer that doesn't have a particular type. It can also have the value NULL in which case it doesn't point to anything.
To use a void pointer, you have to keep track of what it actually points to and when you are going to use it, you must cast it to the appropriate type.
They can be dangerous, because if you cast it to the wrong type, it will result in undefined behavior at runtime.
When something is cast to void, then the value becomes NULL.
Not true. Nothing happens to the pointed data.
Shouldn't a void pointer just be useless?
A void pointer is the closest thing C has to a "generic type" and is very useful in that it allows things like somewhat generic functions, somewhat generic containers etc.
void * is a generic object pointer. Note that there is no generic function pointer.
You can convert any object pointer value to void * and back without loss of value.

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.

Resources