|
Re: sort an array of structures
First you said I had to allocate the structs contiguously. That was the thing that made me confused. But in the second point, you said that the things the pointers point to will not necessarily be in contiguous blocks...
It would have been easier to allocate the structs continuously, because then you would have been able to sort them directly, instead of sorting pointers to pointers.
(But when thinking more of it, if that not had been the case, why should one then need to give the size argument to qsort?)
qsort needs to know the size of the objects it is moving around. It has no way of knowing whether they are pointers, structures, or anything else. So far as it is concerned they are just blocks of memory it is being asked to sort, and it needs to know how large those blocks are.
|