Hey guys (be forewarned that this question makes me feel n00b so I probably am),
I’m able to dynamically create an array and I’m able to use qsort effectively for a statically created array but am having trouble using qsort on a dynamically created one. I think I’m stumbling on my use of pointers.
struct my_struct {
FILE *fp;
int i;
};
So the array contains the above struct and I’d like to sort it by the int value.
Statically, I can do something like this:
struct my_struct array[4];
And sort:
qsort((void *) &array, sizeof(array) / sizeof(struct my_struct), sizeof(struct my_struct), *compare);
—
If I create the array thus:
struct my_struct* = malloc(sizeof(struct process) * 4);
Everything compiles and runs, however execution never goes into the compare function.
Any help would be greatly appreciated
sizeof(array) is (sizeof(struct my_struct) * array_size) for constant-size array, but it’s only pointer size for dynamic one. You have to calculate actual size (one you passed to malloc) yourself and put it into qsort call.