Qsort Error

I am looking at a small C program that uses qsort to sort an array of strings and an error occurs when it passes qsort a function to compare integers instead of strings. Why can't the compiler catch this?
And what would happen if I was using qsort to sort integers and accidentally pass the string comparison function?

So you passed qsort the wrong comparison program and it crashed?

This is natural enough. qsort takes blind, i.e. void types -- the compiler cannot not know what type you are passing into it. It's your job to keep them straight. This is what allows qsort to be flexible.

1 Like

Oh, thank you!
For qsort, if I wanted to check that the algorithm was implemented correctly, what assertions can I put into the code?

It's your job to check input before you put it into qsort.

That depends entirely on what you are doing and what you are trying to do. Lots of test data is a good start, and a known good sort (perhaps the commandline one) to compare your results to.