Code:
int compare (const void * a, const void * b);
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
You only need the prototype if the actual function comes after main (or wherever you call it from).
Code:
char values[500];
char buffer[500];
You declare these as char arrays, but compare and sort integers. Pick one type (probably ints) and stick with it.
Code:
fp = fopen("nums.bin", "r");
Check if fopen failed (fp is NULL) before reading from it. If it's NULL, print an error with perror and exit your program.
Code:
while(fgets(buffer, size, fp) != NULL)
{
values[i] = (int)buffer;
i++;
}
fgets is for text. If nums.bin is binary, as the file extension suggests, you need to use fread instead of fgets. Also, values[i] = (int)buffer assigns every spot in values to contain the address of buffer. fread can read an integer directly into values[i], and if you use fgets, look into sscanf for converting the line of text to ints.
Code:
qsort(values, 4, sizeof(int), compare);
You're telling it to only sort 4 elements. Also, again, you're using sizeof(int), but values is a char array.
Code:
for (n=0; n<sizeof(values); n++){
printf("%d ", values[n]);
}
sizeof(values) will not give you what you want if you convert those arrays to ints (it only works for a char array by virtue of the fact that a char always has a size of 1). You need to give it the number of elements, not the number of bytes all the elements take up.