简体   繁体   English

将一个任意struct指针数组传递给C函数?

[英]Passing an array of arbitrary struct pointers to a C function?

I want to pass an array of arbitrary struct pointers and a comparison function to a generic sorting algorithm. 我想将一个任意struct指针数组和一个比较函数传递给泛型排序算法。 Is this possible in C? 这可能在C?

The goooeys of the structs would only be accessed within the comparison function, the sorting function would only need to call the comparison function and swap pointers, but I can't figure out how to declare it. 结构的goooeys只能在比较函数中访问,sort函数只需要调用比较函数和交换指针,但我无法弄清楚如何声明它。

function sorter( struct arbitrary ** Array, int Length, int cmp(struct node * a, struct node * b))
{
    for (int i=0; i<Length;i++){
        if cmp(Array[i],Array[i+1]){
            swap(Array[i],Array[i+1]
       }
    }
}

You can declare the function as: 您可以将该函数声明为:

void sorter(void** the_array, size_t array_length, int (*comparison_function)(void*, void*));

Inside of the comparison function, you will then need to cast the two pointers being compared to pointers to whatever struct type the comparison function compares. 在比较函数内部,您需要将两个指针进行比较,以指向比较函数比较的任何结构类型的指针。

Actually, this function already exists... it is called qsort . 实际上,这个功能已经存在......它被称为qsort See some documentation here . 在这里查看一些文档。 It's also more efficient than your implementation, which is O(n^2). 它也比你的实现更有效,即O(n ^ 2)。

也许你需要传递无效的指针?

function sorter(void ** Array, int Length, int cmp(void * a, void * b))

It's always possible in C simply because you can convert every pointer to a void* . 它总是可以在C中使用,因为你可以将每个指针转换为void* But, if you want to be able to convert that back to a pointer to an arbitrary structure, you'll need some sort of type identification. 但是,如果您希望能够将其转换回指向任意结构的指针,则需要某种类型标识。

You could do that by using a function specific to the type (if the things you are comparing are the same), or encode the type into the structure somehow. 您可以通过使用特定于该类型的函数(如果您要比较的内容相同)来执行此操作,或者以某种方式将类型编码到结构中。 This can be done by having an extra field in the structure or by changing the cmp() function itself to take a type identifier. 这可以通过在结构中添加额外字段或通过更改cmp()函数本身来获取类型标识符来完成。

But you should be aware that C already has a qsort() function that's usually reasonably efficient (although there's nothing in the standard that dictates what algorithm it uses - it could use bubble sort and still be conforming). 但是你应该知道C已经有一个通常合理有效的qsort()函数(尽管标准中没有任何内容决定它使用什么算法 - 它可以使用冒泡排序并且仍然符合要求)。 Unless you're implementing one for homework, or have a different algorithm in mind, tou should probably just use that. 除非你正在实施一个家庭作业,或者考虑到不同的算法,否则你应该只使用它。

Your algorithm, as it stands, looks like the inner loop of a bubble sort and, as such, won't actually sort correctly. 您的算法,就像它所代表的那样,看起来像是冒泡排序的内部循环,因此实际上不会正确排序。 Bubble sort consists of two nested loops and is generally only suitable for small data sets or those with specific characteristics (such as mostly sorted already). 冒泡排序由两个嵌套循环组成,通常仅适用于小数据集或具有特定特征的数据集(例如主要已排序)。

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

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