解密qsort行为

Jas*_*son 2 c qsort

我需要qsort的功能才能运行我的程序,到目前为止还没有完成它的工作.

我基本上是对单个字符值数组进行排序,以便将它们聚合成组,这样我就可以遍历数组并确定每个属性的计数.我的问题是qsort返回一个"已排序"的数组

xxxxxbxbbbxfbxfbffffbxfxbbfbbbxxfxbxxfbbbbxbfxbxfbxbsxbbbxxbbxxffxbxfxfxbxxbxxfbbbfbxbbx
bbbsxfxbxbxxbfbfxbxxbxxbfxxbxsbfxxfxfxfffxbfxffbbfffsxsfbfbxbxbbbxxsbfbfbbbbbbxxfxfxffxf
xbxxbxfxbfbxbxxbxbxxbxbbffxxbxxffxxbxfxbxffxfsfxxfxxfxxfxfxxfxxbsxxbbbxsxxbbxxxbxfxsbxxx
ffbxfxxffbxxxfxxfxxfxfxxfffbxxxbxxxfffxsbbfffffxxxbbfxsbffxbxxfxbxxfbbfsbffsfffxfxfxbbffx
bxxfxbxxfxbbbfxxbbfxxbbbsxbxfbfbbxxbbfffxxfxxbbbfxxbxxxbbxxxbfxffxxxffxfxxffbxfsxbxxxfxfx
fsbbbxxxbfxfffsfxxxfssxxxfxfxxxxbxbbbxxbxxxxxxxxxxxxxxxxxxxfbfxxffxxbxxxxxxxsxsxxxxxxxxsxb
bxxxxxfxbxxxxfxxfxxxxxbbxfffbxbsxffbbbxsfbbfffbxbfbbxxbxxbbxxbffxfxxfxfbbxxbxfxxsfxxfxxbxf
xxbxxxbxbxbbxbbffxxxxbfbfxxxxxxfxffxxxxxxxxxxxxxxxxxxxxxbxffxbxbxbbxbbxxfbxfxbxxbxxbxbxxxb
xxbxbxbfbbffffffsbbxxbffbxfxxfxbfbfffsxbxxxsxxbbbbbxxxbxxxfxxfffxxxxxxxxxxxxxfxxbxxxxxxxxx
xxbfbxxxxxxxxxxxxxxxxxxxxxxxxxxbxbxxxxxfxxbxxxxffxbxxxxffxfbfffxbxxfxbfxbxxfxbxbfxxxxxfxbx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxsbxxxxffxfxxxxxxxxxfxfxxxbffffxxxfbxbxfxxxxxxxxxxxxxxxxxxxxf
fxfxbfxxxfxxxxx
Run Code Online (Sandbox Code Playgroud)

我认为这个问题与我的函数调用或比较方法有关.

int compare(const void *a, const void *b){
  return *(char * const *) a - *(char * const *) b;
}
Run Code Online (Sandbox Code Playgroud)

并用于

qsort(temp, lineCount, sizeof(char), compare);
Run Code Online (Sandbox Code Playgroud)

where temp是上面的字符数组,是数组中的字符lineCount数.通过测试验证了阵列的完整性以及尺寸.

任何帮助是极大的赞赏.

Nem*_*emo 5

char * const *是指向char的指针.你只需要一个指向char的指针.

尝试:

int compare(const void *a, const void *b){
    return *(const char *) a - *(const char *) b;
}
Run Code Online (Sandbox Code Playgroud)

此外,sizeof(char)根据定义,始终等于1.所以一些C程序员永远不会这样写出来.(这是一个意见问题,它是否使代码更容易阅读,或只是表示你并不真正了解语言.在这种情况下我碰巧喜欢它,但仅仅是FYI.)