I have a lot of records formed by 4 parameters each one (id, field1, field2, field3). Records are contained in a pointer called records
. My goal is to create an InsertionSort function in order to sort these records. The sorting will be done for every parameter of the record (through multiple calling). The trouble is that the function must be general.
What can I write in parameters of InsertionSort function so that the pointer works?
main.c
struct fields{
int id;
char field1[20];
int field2;
float field3;
};
int main() {
struct fields *records = malloc(100000 * sizeof *records);
/* Here , I fill *records with values */
InsertionSort(records,field1,100000); // I order by parameter "field1"
InsertionSort(records,id,100000); // I order by parameter "id"
InsertionSort(records,field2,100000); // I order by parameter "field2"
}
InsertionSort function
void InsertionSort(fields *records, char parameter ,int sizeofrecords) {
int i, j;
void* temp;
for (i = 1; i < size; i++) {
temp = records[i].parameter; //this command doesn't work (records[i])
j = i - 1;
while (j >= 0 && records[j].parameter> temp) {
records[j + 1].parameter= records[j].parameter;
j--;
}
records[j + 1].parameter= temp;
}
}