简体   繁体   English

qsort() 函数如何工作?

[英]How qsort() function works?

void qsort (
    void* base,
    size_t num,
    size_t size,
    int (*compar)(const void*,const void*)
);

How qsort function access "size" byte after adress to get next element? qsort 函数如何在地址后访问“大小”字节以获取下一个元素?
By typecasting void* to char* ?通过将void*类型转换为char*

How does the qsort function access "size" bytes after the base address to get the next element? qsort 函数如何访问基地址后的“size”字节以获取下一个元素? By typecasting void* to char* ?通过将void*类型转换为char*

The assumption inherent in the question is that qsort is itself written in C;问题中固有的假设是qsort本身是用 C 编写的; it need not be.不必如此。 Let's assume that it is.让我们假设它是。

A conversion to char* would be a typical way of doing it, yes, either by explicitly using a cast operator, or by assigning base to a local variable of type char* .转换为char*将是一种典型的方法,是的,通过显式使用强制转换运算符,或通过将base分配给char*类型的局部变量。

GNU C allows you to add an integer to a void pointer directly, but that is a non-portable extension to the C language. GNU C 允许您直接将整数添加到 void 指针,但这是 C 语言的不可移植扩展。

If you have a question about a specific implementation, say what implementation.如果您对特定实现有疑问,请说明是什么实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM