You are passing the array in the wrong way.
it should be
qsort(the_array, 6, sizeof(struct DATA), (compfn)compare );
qsort expects as first argument a pointer to where the data to be sorted starts, and this is the_array
not its address &the_array
since you have declared it this way
DATA *the_array = NULL;
The output would be:
Dave's number is 5!
George's number is 15!
Jim's number is 20!
Bill's number is 33!
Steve's number is 42!
fydo's number is 74!
What happens is that qsort thinks the array is starting at the address of the_array
and start accessing other memory areas where it is not allowed to.
EDIT
I have tryed it with 4000 words and can't get it to crash, here is how i modified the code to read from a file, (strdup is ust handy equivalent to malloc
+ strcpy
)
char name[100];
while(1 == scanf("%s", name)) {
DATA temp;
temp.name = strdup(name);
temp.number = rand();
if (AddToArray(temp) == -1)
return 1;
}
executed like this:
out.exe < lorem.txt
The data is correctly sorted, printed and free-ed