Hey Anduril,
Thank you for the suggestions im having a little bit of problem setting up the main to run and compile the code. This is an assignment and that parameter list is fixed. So i have to use the "compare" function. This code isn't suppose to be ran alone , its compiled along with 2 other files , which gets turned into an executable using a makefile. Anywho here's updated and corrected so far.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void inssrt(void *akeys, int n, int size, int (*compare) (const void * , const void *));
int compare(const void *x,const void *y);
int main()
{
int a[] = {2,56,87,1,4};
int size = 4; //size of int 32bit
int n = 5;
inssrt(&a,n,size,/* PROBLEM HERE */);
}
int compare(const void *x,const void *y)
{
return(* ((int*)x)-*((int *)y));
}
void inssrt(void *akeys, int n, int size, int (*compare) (const void * , const void *))
{
int i,j;
char *keys;
char *tmp;
keys= (char *) akeys;
tmp = (char *) malloc(size*sizeof(char));
for(j=1;j<n;j++) {
memcpy(tmp,&keys[(j)*size],size);
i = j - 1;
while(i >= 0 && (compare(&keys[(i) * size],&keys[(i+1)* size]) < 0)) {
memcpy(&keys[(i+1) * size], &keys[(i) * size],size);
i = i - 1;
}
memcpy(&keys[(i + 1) * size],tmp,size);
free ((char *)tmp);
}
}