6

I'm beginning to learn C and read following code:

public void** list_to_array(List* thiz){
    int size = list_size(thiz);
    void **array = malloc2(sizeof(void *) * size);
    int i=0;
    list_rewind(thiz);
    for(i=0; i<size; i++){
        array[i] = list_next(thiz);
    }
    list_rewind(thiz);
    return array;
}

I don't understand the meaning of void**. Could someone explain it with some examples?

Ravindra S
  • 6,302
  • 12
  • 70
  • 108
mlzboy
  • 14,343
  • 23
  • 76
  • 97

4 Answers4

4

void** is a pointer to a pointer to void (unspecified type). It means that the variable (memory location) contains an address to a memory location, that contains an address to another memory location, and what is stored there is not specified. In this question's case it is a pointer to an array of void* pointers.

Sidenote: A void pointer can't be dereferenced, but a void** can.

void *a[100];
void **aa = a;

By doing this one should be able to do e.g. aa[17] to get at the 18th element of the array a.

To understand such declarations you can use this tool and might as well check a related question or two.

Community
  • 1
  • 1
Ravindra S
  • 6,302
  • 12
  • 70
  • 108
1

void** is a pointer to void*, or a pointer to a void pointer if you prefer! This notation is traditionally used in C to implement a matrix, for example. So, in the matrix case, that would be a pointer to an array of pointers.

cybertextron
  • 10,547
  • 28
  • 104
  • 208
0

Normally void * pointers are used to denote a pointer to an unknown data type. In this case your function returns an array of such pointers thus the double star.

In C, a pointer is often used to reference an array. Eg the following assignment is perfectly legal:

char str1[10];
char *str2 = str1;

Now when void is used, it means that instead of char you have a variable of unknown type.

Pointers to an unknown data type are useful for writing generic algorithms. Eg. the qsort function in standard C library is defined as:

void qsort ( void * base, 
             size_t num, 
             size_t size, 
             int ( * comparator ) 
             ( const void *, const void * ) );

The sorting algorithm itself is generic, but has no knowledge of the contents of the data. Thus the user has to provide an implementation of a comparator that can deal with it. The algorithm will call the comparator with two pointers to the elements to be compared. These pointers are of void * type, because there is now information about the type of data being sorted.

Take a look at this thread for more examples http://forums.fedoraforum.org/showthread.php?t=138213

anttix
  • 7,709
  • 1
  • 24
  • 25
  • 3
    -1 for saying that `char **strarray2 = strarray1;` is "perfectly legal". – Jens Gustedt Jul 12 '12 at 04:44
  • Jens is right, `char[][]` is equivalent to `char[]` (it's just convenience syntax that lets the compiler do 2D array pointer arithmetic for you), while a `char**` is an array of pointers to char arrays. – Adam Jul 12 '12 at 05:09
  • Thanx for pointing that out, I did not have a C compiler at hand when I answered it to correct me :( – anttix Jul 13 '12 at 02:16
0

void pointers are used to hold address of any data type. void** means pointer to void pointer. Void pointers are used in a place where we want a function should receive different types of data as function argument. Please check the below example

void func_for_int(void *int_arg)
{
    int *ptr = (int *)int_arg;
    //some code
}

void func_for_char(void *char_arg)
{
    char *ptr = (char *)char_arg;
    //some code
}

int common_func(void * arg, void (*func)(void *arg))
{
    func(arg);
}

int main()
{
    int a = 10;
    char b = 5;

    common_func((void *)&a, func_for_int);
    common_func((void *)&b, func_for_char);

    return 0;
}
rashok
  • 12,790
  • 16
  • 88
  • 100