What does method(void * param) mean in C? - 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.

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.

Assigning the value from the void pointer to a non void pointer

I have a situation where in the address inside the void pointer to be copied to a another pointer. Doing this without a type cast gives no warnings or errors. The pseudo code looks like
structA A;
void * p = &A;
structA * B = p;// Would like to conform this step
I don't foresee any problems with this.
But since this operation is used over a lot of places, I would like to conform whether it can have any replications. Also is there any compiler dependency?
No, this is fine and 100% standard.
A void * can be converted to and from any other data/object pointer without problems.
Note that data/object restriction: function pointers do not convert to/from void *.
This is the reason why you shouldn't cast the return value of malloc(), which returns a void *.
In C a void * is implicitly compatible with any pointer. That why you don't need to cast when e.g. passing pointers of any type to functions taking void * arguments, or when assigning to pointer (still of any type) from function returning void * (here malloc is a good example).

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

Confusion regarding a function definition in C

I found this function definition
void *func(void *param) {
}
Actually, I have a certain confusion regarding this function definition. What does void * mean in the return type of the function as well as the argument. I am a beginner in C. So please don't mind. Thank you
void *func(void *param) {
int s = (int)param;
....
}
Well looking at the above program which I found. I think it should have been
int *s = (int *)param;
isn't it? I am confused
void * means it's a pointer of no specific type, think of it as a generic pointer, unlike say int * an int pointer.
You can cast it into a different type if need be (for instance if you are going to do pointer arithmetic with the pointer).
You might find this SO question of use: Concept of void pointer in C programming
It simply means that the function func takes one parameter which is a pointer (to anything) and returns a pointer (to anything). When you use a void *, you are telling the compiler "this is a pointer, but it doesn't matter at this point what it's a pointer to".
When you want to actually use the data it's pointing to, you need to cast it to a pointer to some other type. For example, if it points to an int, you can create an int * variable and dereference that:
int *int_ptr = (int *)param;
// now (*int_ptr) is the value param is pointing to
you can think of it as a raw pointer, nothing more than an address, think about it, pointers are nothing more than address right, so they should all be of equal size, either 32 or 64 bits in most modern systems but if thats the case why do we say int * or char * or so on if they are all the same size, well thats because we need of a way to interpret the type we are pointing to, int * means when we dereference go to that address and interpret the 4 bytes as an int or char * means when we dereference go to that address and get a byte, so what happens when you dereference a void * well you get warning: dereferencing ‘void *’ pointer basically we really can't and if you do its affects are compiler dependent, the same applies when we do arithmetics on it.
So why bother using them? well I personally don't and some groups dont particularly like them, fundamentally they allow you to create fairly generic subroutines, a good example would be memset which sets a block of memory to a certain byte value, its first argument is a void * so it won't complain whether giving a char * or int * due note that it works on a per byte basis, and you need to properly calculate the total size of the array.
void *func(void *param) {
}
param is a void pointer means it is a pointer of any reference type. since a void pointer has no object type,it cannot be dereferenced unless it is case.
so void *param;
int *s=(int*)param;
here since param is an pointer variable so you will have to cast it as a pointer variable.not to a int type as you did there.
e.g.
int x;
float r;
void *p=&x;
int main()
{
*(int *)p=2;
p=&r;
*(float*)p=1.1;
}
in this example p is a void pointer. now in main method I have cast the p as a int pointer and then as a float pointer so that it can reference to first a integer and then a float.
Returning a pointer of any type. It can be of any datatype.

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