0

I have to sort this :

char ** lu

It contains line like this:

807 qmuftnqrjalreihiqblaplydrixyvzenlasfojyxlloptyakbnmqymwputqstufguylkjlkux

Each line have a number at the beginning, i need to sort them with it

But i dont know how to use strtol is the cmp_fct of quicksort

Code sample:

    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;
    }

How I use Quicksort :

qsort(lu, nlu, sizeof(lu[0]), int_cmp);

nlu is the number of line to be sorted

I think i have to use a and b in strtol(). but strtol show me wrong index (0 or 1)

Prototype of strtol(): long int strtol(const char *str, char **endptr, int base)

PROBLEME HERE:(for the test of int_comp)

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

Thanks for help

  • Please show a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Jabberwocky Mar 17 '16 at 15:30
  • Edited to be more specific –  Mar 17 '16 at 15:37
  • 1
    I am pretty sure a `char **` does not "contain" what you wrote. If you're lucky, it points to a pointer pointing to what you wrote. Details are important that way, which is why we are asking for a minimal, **complete and verifiable** example of what you tried -- `int_cmp()`, `int main()` with definition of `lu` (including a couple of example records) and call to `qsort()`. – DevSolar Mar 17 '16 at 15:38
  • Thank you for your answer, i gave the full codel, only the compare fonction don't work –  Mar 17 '16 at 15:43

1 Answers1

0

The qsort() comparison callback for strings beginning with decimal integers should be something like:

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;
} 

Don't use subtraction in the return, that's open to integer overflow errors.

Also, please don't cast the return value of malloc() in C.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thank for answerring but It dont work, same as mine : 'ia =0 ib =0 ia =1 ib =0 ia =1 ib =0 ia =0 ib =0 ia =0' –  Mar 17 '16 at 16:00
  • Thanks for the advice, I did what you said, but i still have the same problem as before, ia and ib dont get the correct value –  Mar 17 '16 at 16:10