void pointer

hi guys!

Is there such a thing as double void pointer dynamic allocation?
And if so is it something like this?

int n;
void** a;
a=malloc(n*sizeof(void*));

The void does datatype does not have a size, so that will not work. Yes, void ** can exist.

What are you trying to accomplish?

I want to have n void*.
But n is an argument to the function,that's why I thought it would be better if I had an array of void*

The size of void may be undefined(not always -- sometimes it's '1' for the sake of easy pointer math), but the size of void * is definitely defined -- it's the same size as any other pointer.

I don't see any reason you couldn't have an array of void * pointers, and don't see anything wrong with the code you posted..

The thing is that when I try to refer to one element of the array,that means to one void* it doesn't work.
For example when I had:

main(){
    void* a;
    fun(&a); //call a function named fun
}

void fun(void** b){
//do things
}

it worked fine.But when I did this for many void* in a loop

main(){
   void** a;
   a=(void**)malloc(n*sizeof(void*));
   for(int i=0;i<n;i++)
      fun(a);//call the same function as above
}

void fun(void **b){
  //do things
}

it returns segmentation fault.

Dont know what code is inside fun but you cant dereference a void*...besides a [i]is a void* not void** so definition of fun needs to change to "void fun(void *b) {...}"

1 Like

Actually I changed the

a 

to

&a

and works fine.
One more question,if I want to send in a function not only one element:

&a

but all of them do I simply do:

funct(a);//a is the void**

and if so ,the function's argument is:

void funct(void** a){
   int cc;
   for(int i=0;i<n;i++)
      memcpy(&cc,*a+sizeof(int),sizeof(int));//and this is how we reffer to each void* pointer of the group?

}

It's still wrong. The compiler can't understand your intent here, it can only tell you when something's syntactically wrong.

If you want to pass the array element and not the array itself, that's a void *, not a void **.

That's strange because not only compiles fine but works as well...
So if I want to pass the whole array I have to do it the way I said before?

It is actually correct since &a [i]is void** not void*...unless i am not gettng your drift...

It might help if you posted the contents of // do things

I've seen situations in C where two opposite mistakes made the code work fine. I'm still not convinced the code works quite the way you think it does.

It's syntactically correct, but that doesn't mean it's logically correct. That he's taking the array index makes me suspect he wants the content of the array, but the ampersand makes it a pointer pointer again...

It works because you got it right...

To pass whole array supply its name as argument to fun like "fun(a)"...even fun(&a[0]) works since array name "a" is a pointer to a[0]...

---------- Post updated at 03:15 PM ---------- Previous update was at 03:10 PM ----------

Ah my bad :eek: since you are hinting at the semantics...