What does (void **) mean in C?

I would look at this, but to be honest, I don’t know where to start, because I don’t know what it is called. I saw variables passed to such functions:

myFunction((void**)&variable); 

Which confuses me because all my acquaintances are familiar to me; I had never seen them together before.

What does it mean? I'm a newbie, so the less jargon, the better, thanks!

+6
c ++ c
source share
7 answers

This is a cast to a pointer to a void pointer.

You often see this with features like CoCreateInstance() on Windows systems.

 ISomeInterface* ifaceptr = 0; HRESULT hr = ::CoCreateInstance(CLSID_SomeImplementation, NULL, CLSCTX_ALL, IID_ISomeInterface, (void**)&ifaceptr); if(SUCCEEDED(hr)) { ifaceptr->DoSomething(); } 

Listing converts a pointer to an ISomeInterface pointer to a pointer to a void pointer, so CoCreateInstance() can set ifaceptr valid value.

Since it is a pointer to a void pointer, the function can output pointers of any type depending on the interface identifier (for example, IID_ISomeInterface).

+4
source share

void* is a "pointer to anything." void ** is another level of indirection - "a pointer to a pointer to anything." Basically, you pass this when you want the function to return a pointer of any type.

&variable accepts the address of a variable. variable should already be something like a pointer to work, but probably not void * - it could be, say, int * , so its address will lead to int ** . If the function accepts void ** , then you need to apply it to this type.

(Of course, it must really return an object of the correct type, otherwise the call to the code will fail on the track when it tries to use it incorrectly.)

+7
source share

This is a pointer to a pointer to a variable of undefined type. All pointers are the same size, so void* just means "pointer to something, but I don't know what it is." A void** can also be a two-dimensional array of an unspecified type.

+3
source share

Drops &variable to void** (i.e. a pointer to a pointer to void ).

For example, if you have something in the lines

 void myFunction(void** arg); int* variable; 

This passes the address of variable (what unary & does; it accepts the address) to myFunction() .

+1
source share

Divide it in parts ...

myFunction takes a pointer to a pointer of type void (which pretty much means that it can point to anything). It could be declared something like this:

 myFunction(void **something); 

Everything you go through must be of this type. So you take the address of the pointer and drop it (void **) to make it a pointer to void. (Basically, depriving him of any idea of ​​what he points to - which the compiler might whine otherwise.)

This means that the & variable is the address (& does this) of the pointer, so the variable is the pointer. To what? Who knows!

Here's a more complete snippet to give an idea of ​​how this fits together:

 #include <stdio.h> int myInteger = 1; int myOtherInt = 2; int *myPointer = &myInteger; myFunction(void **something){ *something = &myOtherInt; } main(){ printf("Address:%p Value:%d\n", myPointer, *myPointer); myFunction((void**)&myPointer); printf("Address:%p Value:%d\n", myPointer, *myPointer); } 

If you compile and run this, it should provide this output:

 Address:0x601020 Value:1 Address:0x601024 Value:2 

You can see that myFunction changed the value of myPointer - which it could only do because the pointer address was passed to it.

+1
source share

A variable is a pointer to something like undefined (void). The operator returns the address of this variable, so now you have a pointer to a pointer to something. Therefore, the pointer is passed to the function by reference. A function may have a side effect that changes the memory referenced by this pointer. In other words, calling this function can change what the source pointer refers to.

0
source share
 #include <stdio.h> #include <iostream> int myInteger = 1; std::string myOtherInt = "Test"; int *myPointer = &myInteger; void myFunction(void **something) { *something = &myOtherInt; } int main(){ //printf("%d\n", *myPointer); printf("Address:%p Value:%d\n", myPointer, *myPointer); myFunction((void**)&myPointer); printf("Address:%p Value:%d\n", myPointer, *myPointer); } 
0
source share

All Articles