Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the declaration void** mean in the C language?

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?

like image 884
mlzboy Avatar asked Jul 12 '12 01:07

mlzboy


People also ask

What does void * mean in C?

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.

What is void declaration in C?

In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.

What type is a void * in C?

The void type, in several programming languages derived from C and Algol68, is the return type of a function that returns normally, but does not provide a result value to its caller. Usually such functions are called for their side effects, such as performing some task or writing to their output parameters.

What is void * p?

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


1 Answers

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.

like image 88
Pale Blue Dot Avatar answered Oct 09 '22 14:10

Pale Blue Dot