先说说我这个程序的场景。我程序中有一个二维数组,代码段如下:
char files[101][64]; // files[i][0] sotres the length of the i-the file name
正如注释中说的, files[i ][0] 用来存储 files[i ] 这个字符串的长度,字符串是从 files[i ][1] 开始存储的,每个字符串长度保证不超过 60, 所以才考虑这样来存储。现在突然发现在字符串都存储到 files 中去后,必须还要对 files 中的字符串进行排序。如果是使用 c++ 的 string 来写当然没问题,但是既然代码已经写成这样,就没再重写,而是打算用 C 的 qsort 来排序,这也是一个很有挑战性的工作。
对于自己的代码,先后尝试了很多办法也没能让它正确工作,最终想到调试 MSDN 上给出的那个使用 qsort 对字符串进行排序的程序。那个程序如下:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int compare( const void *arg1, const void *arg2 );
void main( int argc, char **argv )
{
int i;
char files[101][64]; // files[i][0] sotres the length of the i-the file name
正如注释中说的, files[i ][0] 用来存储 files[i ] 这个字符串的长度,字符串是从 files[i ][1] 开始存储的,每个字符串长度保证不超过 60, 所以才考虑这样来存储。现在突然发现在字符串都存储到 files 中去后,必须还要对 files 中的字符串进行排序。如果是使用 c++ 的 string 来写当然没问题,但是既然代码已经写成这样,就没再重写,而是打算用 C 的 qsort 来排序,这也是一个很有挑战性的工作。
对于自己的代码,先后尝试了很多办法也没能让它正确工作,最终想到调试 MSDN 上给出的那个使用 qsort 对字符串进行排序的程序。那个程序如下:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int compare( const void *arg1, const void *arg2 );
void main( int argc, char **argv )
{
int i;