我想在C中對任意類型的數組進行排序我正在使用空指針和memcpy指針。但是,我不斷收到錯誤,說明與memcopy函數關聯的「無效使用void表達式」,以及「void指針無效解引用」。我知道還有很多關於這個錯誤的其他線索,但是閱讀這些線程並沒有幫助我解決這個問題。我的代碼如下:無效使用memcpy在一個任意類型的空表達
#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'爲你'數組[d]做' – ForceBru
應該如何編譯器現在什麼類型它shoule被解除引用? (應該使用多少個字節?) – Keammoort