Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (void**) mean in C?

Tags:

c++

c

I would look this up, but honestly I wouldn't know where to start because I don't know what it is called. I've seen variables passed to functions like this:

myFunction((void**)&variable);

Which confuses the heck out of me cause all of those look familiar to me; I've just never seen them put together like that before.

What does it mean? I am a newb so the less jargon, the better, thanks!

like image 652
numerical25 Avatar asked May 31 '10 03:05

numerical25


People also ask

What does void *) 0 represent?

(void*)0 is a null pointer constant, whose value is a null pointer of type void* , so by the semantics of parenthesized expressions ((void*)0) also has a value that is a null pointer of type void* .

What does a void pointer mean in C?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is void data in C?

There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void.

What does void star mean in C?

A void pointer(void* ) is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type.


2 Answers

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

&variable takes the address of variable. variable should already be some kind of a pointer for that to work, but it's probably not void * - it might be, say int *, so taking its address would result in a int **. If the function takes void ** then you need to cast to that type.

(Of course, it needs to actually return an object of the right type, otherwise calling code will fail down the track when it tries to use it the wrong way.)

like image 107
EMP Avatar answered Oct 21 '22 03:10

EMP


Take it apart piece by piece...

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

myFunction(void **something);

Anything you pass in has to have that type. So you take the address of a pointer, and cast it with (void**) to make it be a void pointer. (Basically stripping it of any idea about what it points to - which the compiler might whine about otherwise.)

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

Here is 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 give this sort of 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 it was passed the address of the pointer.

like image 45
Kim Reece Avatar answered Oct 21 '22 05:10

Kim Reece