C->qsort函数

0x1000 2008-11-28 09:45:42
求qsort这个函数的详解;
另外,qsort这个函数是在stdlib.h中声明的,具体的函数体在哪里?
谢谢!
...全文
187 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jznhljg 2008-11-29
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */

return strcmp(* (char * const *) p1, * (char * const *) p2);
}

int
main(int argc, char *argv[])
{
int j;

assert(argc > 1);

qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp);

for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}
xxgamexx 2008-11-28
  • 打赏
  • 举报
回复
man qsort 出来的!!


QSORT(3) Linux Programmer’s Manual QSORT(3)

NAME
qsort - sorts an array

SYNOPSIS
#include <stdlib.h>

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



0x1000 2008-11-28
  • 打赏
  • 举报
回复
如果各位大侠能在这里给出实现代码和解释是最好!
谢谢各位!
0x1000 2008-11-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 WingForce 的回复:]
lz的意思大概是实现在哪里,对于windows平台来说,它属于Microsoft C runtime library,根据不同的编译选项,可能使用以下文件中的2进制实现

libcmt.lib
msvcrt.lib
libcmtd.lib
msvcrtd.lib
msvcmrt.lib
msvcurt.lib

msvcm80.dll
msvcr80.dll
msvcr80d.dll
msvcm80.dll
[/Quote]
我用gcc编译器——Linux下
WingForce 2008-11-28
  • 打赏
  • 举报
回复
lz的意思大概是实现在哪里,对于windows平台来说,它属于Microsoft C runtime library,根据不同的编译选项,可能使用以下文件中的2进制实现

libcmt.lib
msvcrt.lib
libcmtd.lib
msvcrtd.lib
msvcmrt.lib
msvcurt.lib

msvcm80.dll
msvcr80.dll
msvcr80d.dll
msvcm80.dll

就呆在云上 2008-11-28
  • 打赏
  • 举报
回复
void qsort(
void *base,
size_t num,
size_t width,
int (__cdecl *compare )(const void *, const void *)
);

base
Start of target array.

num
Array size in elements.

width
Element size in bytes.

compare
Comparison function. The first parameter is a pointer to the key for the search and the second parameter is a pointer to the array element to be compared with the key.



在<stdlib.h> 里面

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧