Reputation: 2036
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
Upvotes: 3
Views: 207
Reputation: 143047
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
Upvotes: 5
Reputation: 1508
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.
Upvotes: 0
Reputation: 11120
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.
Upvotes: 1
Reputation: 57248
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
Upvotes: 2