Sorting an Array
Now that we know how to structure our data, we can revisit the signature of qsort:
|||comparator:CFuncPtr2[Ptr[Byte], Ptr[Byte], Int])|
We’ll pass in an array of NGramData cast to Ptr[Byte], we can get the value of num from the used field of the WrappedArray, and we can get the size of each item just by sizeof[NGgramData]. But for qsort to work, we have to supply the comparator, too; and comparator has a type we haven’t seen before, a CFuncPtr, or function pointer.
Implementing a Comparator
In a typical systems programming course, abstracting from functions to function pointers is one of the hardest conceptual leaps, and C’s syntax certainly doesn’t make it any easier. In Scala ...