c - 在任意排序中无效使用带有 memcpy 的 void 表达式

标签 c arrays sorting

我试图在 C 中对任意类型的数组进行排序。我正在使用指向 voids 和 memcpy 的指针。但是,我不断收到错误消息,指出与 memcopy 函数相关的“无效使用 void 表达式”和“无效取消引用 void 指针”。我知道还有很多关于此错误的其他线程,但阅读这些线程并没有帮助我解决问题。我的代码如下:

#include "mysort.h"
#include <alloca.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

void mysort(int n, int elementSize, void * array, int ascending,CompareFunction compFunc)
{
    //bubblesort algorithm
    if (ascending == 1) 
    {   int c,d;
        void * swap = malloc(elementSize);
        void * thing1 = malloc(elementSize);
        void * thing2 = malloc(elementSize);

        for (c = 0; c < (n - 1); c++)
        {
            for (d = 0; d < n - c - 1; d++)
            {
                memcpy(thing1, array[d], elementSize);
                memcpy(thing2, array[d+1], elementSize);
                if ( compFunc(thing1,thing2) >= 0) 
                    {
                        memcpy(swap, array[d], elementSize);
                        array[d] = array[d+1];
                        array[d+1]= swap;
                    }
            }
        }
    }   


    if (ascending != 1) 
    {   int c,d;
        void * swap = malloc(elementSize);
        void * thing1 = malloc(elementSize);
        void * thing2 = malloc(elementSize);

        for (c = 0; c < (n - 1); c++)
        {
            for (d = 0; d < n - c - 1; d++)
            {
                memcpy(thing1, array[d], elementSize);
                memcpy(thing2, array[d+1], elementSize);
                if ( compFunc(thing1,thing2) <= 0) 
                    {
                        memcpy(swap, array[d], elementSize);
                        array[d] = array[d+1];
                        array[d+1]= swap;
                    }
            }
        }
    }   
}

如有任何建议,我们将不胜感激。

最佳答案

您不能像使用已知类型的数组那样在 void * 上使用数组下标运算符。您需要转换为 char * 并自己进行指针运算。

此外,您不需要 thing1thing2。只需在要比较的元素上直接调用 compFunc

所以改变这个:

            memcpy(thing1, array[d], elementSize);
            memcpy(thing2, array[d+1], elementSize);
            if ( compFunc(thing1,thing2) >= 0) 
                {
                    memcpy(swap, array[d], elementSize);
                    array[d] = array[d+1];
                    array[d+1]= swap;
                }

对此:

            void *current = (char *)array + (elementSize * d);
            void *next = (char *)array + (elementSize * (d + 1));
            if ( compFunc(current, next) >= 0)
                {
                    memcpy(swap, current, elementSize);
                    memcpy(current, next, elementSize);
                    memcpy(next, swap, elementSize);
                }

其他情况也类似。

关于c - 在任意排序中无效使用带有 memcpy 的 void 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340076/

相关文章:

c - 确定包含的库头文件使用了哪些预处理器定义

c - 无法终止并发运行的进程

c - 使用指针将一个数组分配给另一个数组

wordpress - Woocommerce,对基于短代码的产品列表进行排序下拉

python - 按值排序后如何按字母顺序对字典的键进行排序?

C qsort 奇怪的行为

c - 在 C 语言中, `&function` 和 `function` 作为参数传递时有什么区别?

c - 在C中的函数中打印数组中的字符串

C - 结构体指针数组,语法

sorting - 使用 sort with key in racket 对功能列表进行排序