c - 如何调用以指针数组作为参数的函数?在C中

标签 c arrays pointers linked-list

我需要在O(n*logk)中合并K个有序列表。我正在尝试创建一个列表数组,但我不知道如何调用某些函数(例如 min_heapify)。

void min_heapify(List *list[],int size);

void main() {
    List list[10]; // initialize all 10 list randomly in ascending order.
    min_heapify(&list,10); // This line won't compile.

}

请帮忙。

最佳答案

您正在传递一个指向数组的指针,而不是传递函数所需的指针数组。

您的函数需要一个列表指针数组,因为它有一个星号和一对括号:

List *list[] // <<== This means "an array of pointers to elements of type List"

要向该函数传递一个指针数组,请在 main 中声明一个指针数组,然后进行调用:

void main() {
    List *list[10];
    // intitialize all 10 list pointers randomly in ascending order.
    for (int i = 0 ; i != 10 ; i++) {
        list[i] = malloc(...); // Initialize list at position i
    }
    min_heapify(list,10); // Remove the ampersand
}

如果您希望传递列表数组,请仅保留星号或方括号:

void min_heapify(List list[],int size);

或同等内容

void min_heapify(List *list,int size);

main 的调用中删除 & 符号以正确编译:

void main() {
    List list[10]; // intitialize all 10 lists randomly in ascending order.
    min_heapify(list,10); // Remove the ampersand
}

关于c - 如何调用以指针数组作为参数的函数?在C中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22732840/

相关文章:

C Pthread : Possible Deadlock, 线程 1 挂起而线程 2 退出

arrays - 做一个 O(1) 操作,O(n) 次还是 O(1)?

c++ - 你不能用双指针引用二维数组吗?

c - 复制结构时没有获得正确的值

c - 如何在 Visual Studio 2015 中将 header 编译器更改为 C 编译器(不是 C++)

c - 在 C 中表示单个位

java - 使用扫描仪输入类处理可变数量的参数

c - 为什么 malloc(sizeof(pointer)) 有效?

C:打印的字符不正确,怀疑是内存。问题

javascript - 使用 querySelector 在 for 循环中获取属性