Bug in quicksort example (K&R C book)?

后端 未结 4 867
有刺的猬
有刺的猬 2021-02-15 12:50

This quicksort is supposed to sort \"v[left]...v[right] into increasing order\"; copied (without comments) from The C Programming Language by K&R (Second Edition):



        
4条回答
  •  佛祖请我去吃肉
    2021-02-15 13:43

    K&R was always a bit sloppy with their use of unsigned vs signed arguments. Side effect of working with a PDP that had only 16 kilobytes of memory I suppose. That's been fixed a while ago. The current definition of qsort is

    void qsort(
       void *base,
       size_t num,
       size_t width,
       int (__cdecl *compare )(const void *, const void *) 
    );
    

    Note the use of size_t instead of int. And of course void* base since you don't know what kind of type you are sorting.

提交回复
热议问题