简体   繁体   English

声明void **在C语言中是什么意思?

[英]What does the declaration void** mean in the C language?

I'm beginning to learn C and read following code: 我开始学习C并阅读以下代码:

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** . 我不明白void**的含义。 Could someone explain it with some examples? 有人可以举例说明吗?

void** is a pointer to a pointer to void (unspecified type). void **是指向void(未指定类型)的指针。 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. 在这个问题的情况下,它是一个指向void *指针数组的指针。

Sidenote: A void pointer can't be dereferenced, but a void** can. 旁注:不能取消引用void指针,但可以取消void **。

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

By doing this one should be able to do eg aa[17] to get at the 18th element of the array a. 通过这样做,应该能够做到例如aa [17]到达数组a的第18个元素。

To understand such declarations you can use this tool and might as well check a related question or two . 要了解此类声明,您可以使用工具,并且不妨检查一个或两个相关问题

void** is a pointer to void* , or a pointer to a void pointer if you prefer! void**是指向void*pointer ,或者,如果您愿意,则pointer void pointer This notation is traditionally used in C to implement a matrix, for example. 例如,传统上在C使用此符号来实现矩阵。 So, in the matrix case, that would be a pointer to an array of pointers . 因此,在矩阵的情况下,那将是一个pointer array of pointers

Normally void * pointers are used to denote a pointer to an unknown data type. 通常, void *指针用于表示指向未知数据类型的指针。 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. 在C语言中,经常使用指针来引用数组。 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. 现在,当使用void时,这意味着您将拥有一个未知类型的变量而不是char。

Pointers to an unknown data type are useful for writing generic algorithms. 指向未知数据类型的指针对于编写通用算法很有用。 Eg. 例如。 the qsort function in standard C library is defined as: 标准C库中的qsort函数定义为:

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. 这些指针是void *类型的,因为现在有了有关正在排序的数据类型的信息。

Take a look at this thread for more examples http://forums.fedoraforum.org/showthread.php?t=138213 看一下该线程以获取更多示例http://forums.fedoraforum.org/showthread.php?t=138213

void pointers are used to hold address of any data type. void指针用于保存任何数据类型的地址。 void** means pointer to void pointer. void**表示指向void指针的指针。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM