我需要在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/