C QSort索引字符**

C QSort索引字符**,c,char,quicksort,C,Char,Quicksort,我得把这件事分类: 字符**lu 它包含如下行: 807 qmuftnqrjalreihiqblaplydrixyvzenlasfojyxloptyakbnmqmwputqstufguylkjlkux 每行开头都有一个数字,我需要用它对它们进行排序 但我不知道如何使用strtol是快速排序的核心 代码示例: char ** lu = NULL; int nlu = 0; size_t n_byte = 10000*8; // 10000 octects par ligne

我得把这件事分类:

字符**lu

它包含如下行:

807 qmuftnqrjalreihiqblaplydrixyvzenlasfojyxloptyakbnmqmwputqstufguylkjlkux

每行开头都有一个数字,我需要用它对它们进行排序

但我不知道如何使用strtol是快速排序的核心

代码示例:

    char ** lu = NULL;
    int nlu = 0;
    size_t n_byte = 10000*8; // 10000 octects par ligne cf:énoncé

    void lire(FILE * from){ //rm data.out && ./a.out < data.in > data.out && cmp -b data.in data.out

      char* ligne = malloc(n_byte);
      lu = malloc(n_byte);
      assert(lu != NULL);
      assert(ligne != NULL);


      while(getline(&ligne, &n_byte, from) != -1) // getline return -1 on failure to read a line (including end-of-file condition)
      {
        if (ligne[strlen(ligne) - 1] != '\n')
          fprintf(stderr, "Aie ! Ligne trop longue (%d max)\n", MaxCar);

        else
          ligne[strlen(ligne) - 1] = '\0';

        if (nlu == MaxLig)
        {
          fprintf(stderr, "Aie ! Trop de lignes (%d max)\n", MaxLig);
          break;
        }

        lu[nlu] = malloc(n_byte);
        assert(lu != NULL);
        assert(lu[nlu] != NULL);
        memcpy(lu[nlu], ligne, n_byte); /* /?\ + standard que strdup /?\*/

        if (lu[nlu] == NULL){
          fprintf(stderr, "Aie ! Plus de mémoire\n");
          exit(1);
        }
        nlu += 1;
      }
    }

    static int int_cmp(const void *a, const void *b) 
{ 
   const long ia = strtol(a, NULL, 10); // casting pointer types 
   const long ib = strtol(b, NULL, 10); // casting pointer types 
   printf("\n ia =%ld", ia);
   printf("\n ib =%ld", ib);
   if(ia < ib)
        return -1;
   return ia > ib;
} 

    void
    ecrire(FILE * to){
      int i;

      for(i = 0; i < nlu; i++)
        printf("%s\n", lu[i]);
    }

    int
    main(int ac, char * av[]){
      lire(stdin);
      qsort(lu, nlu, sizeof(lu[0]), int_cmp);
      ecrire(stdout);
      return 0;
    }
nlu是要排序的行数

我想我必须在strtol()中使用ab但是strtol显示了错误的索引(0或1)

strtol()的原型:
long int strtol(const char*str,char**endptr,int base)

这里的问题:(用于测试int\u comp)

ia=0 ib=0 ia=0 ib=0 ia=0 ib=0 ia=0

感谢您的帮助

对于以十进制整数开头的字符串,
qsort()
比较回调应该类似于:

static int int_cmp(const void *a, const void *b) 
{
  const long la = strtol(a, NULL, 10);
  const long lb = strtol(b, NULL, 10);
  if(la < lb)
    return -1;
  return la > lb;
} 
static int\u cmp(常量无效*a,常量无效*b)
{
const long la=strtol(a,NULL,10);
const long lb=strtol(b,NULL,10);
如果(lalb;
} 
不要在
返回中使用减法,否则会出现整数溢出错误


另外,.

请显示一个.Edited以更具体。我很确定
char**
不“包含”您所写的内容。如果你幸运的话,它会指向一个指向你所写内容的指针。通过这种方式,细节非常重要,这就是为什么我们要求您提供一个最小、完整且可验证的示例--
int\u cmp()
int main()
,具有
lu
定义(包括一些示例记录)并调用
qsort()
。感谢您的回答,我给出了完整的代码,只有比较功能不起作用谢谢你的回答但它不起作用,和我的一样:“ia=0 ib=0 ia=1 ib=0 ia=1 ib=0 ia=0 ib=0 ia=0 ia=0”谢谢你的建议,我照你说的做了,但我仍然有和以前一样的问题,ia和ib没有得到正确的值
static int int_cmp(const void *a, const void *b) 
{
  const long la = strtol(a, NULL, 10);
  const long lb = strtol(b, NULL, 10);
  if(la < lb)
    return -1;
  return la > lb;
}